patternjavascriptMinor
JavaScript "recursion" via setTimeout
Viewed 0 times
javascriptviarecursionsettimeout
Problem
I understand that because of JavaScript's single-threaded execution, long loops or recursive calls could make the browser unresponsive while they execute.
I thought about simulating recursion using
Here's an example comparing a standard recursive factorial function with an attempt to do the same thing but using
I tested this using the following test:
This worked for that range of numbers, although for higher numbers the two functions seem to give different results. for example, for 100,
I have two questions:
I thought about simulating recursion using
setTimeout with a delay of 0, so as to queue the next call to execute as soon as possible.Here's an example comparing a standard recursive factorial function with an attempt to do the same thing but using
setTimeout:function rFactorial(num)
{
if (num === 0) return 1;
else return num * rFactorial( num - 1 );
}
function tFactorial(num, callback, acc) {
acc = acc || 1;
if (num === 0) {
callback(acc);
}
else {
setTimeout(function() {
tFactorial(num-1, callback, acc*num);
}, 0);
}
}I tested this using the following test:
for (var x = 1; x < 20; x += 1) {
tFactorial(x, (function(n) {
return function(result) {
console.log(rFactorial(n) === result)
};
})(x));
}This worked for that range of numbers, although for higher numbers the two functions seem to give different results. for example, for 100,
sFactorial gives 9.332621544394418e+157, while rFactorial gives 9.33262154439441e+157.I have two questions:
- Is this a good approach? Can anyone suggest any improvements etc?
- Can anyone shed any light on why the two functions give the same results for lower numbers but different results for higher numbers?
Solution
A few observations :
Numbers in javascript are IEE754 double precision float. All of them. There is no integer type. Which means that only 53 bits are available to describe the integer part. So it doesn't make sense to try to do integer operations on numbers bigger than that. Your numbers are wayyy too big and the results will be undefined. If you want to compute bigger numbers, you'll need to define your own format, you can't use the native numbers of javascript.
Using
Numbers in javascript are IEE754 double precision float. All of them. There is no integer type. Which means that only 53 bits are available to describe the integer part. So it doesn't make sense to try to do integer operations on numbers bigger than that. Your numbers are wayyy too big and the results will be undefined. If you want to compute bigger numbers, you'll need to define your own format, you can't use the native numbers of javascript.
Using
setTimeout or using a recursion might be amusing solutions but they're not terribly efficient (at least until we have tail call optimization in javascript). You'd better use a simple boring loop.Context
StackExchange Code Review Q#21176, answer score: 3
Revisions (0)
No revisions yet.