Crunch Factorial Example

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example: 5! = 5 x 4 x 3 x 2 x 1 = 120

Calculate Factorial via Web Worker

Crunch is loaded as a web worker, new Worker("crunch.js"). Every calculation is requested with a message: worker.postMessage({"func": "mul", "args": [iteration, result]}).

You can chose between the iterative and native implementation. The iterative will return a result at each step, and is slower as decimal conversions are done each time, or the native, which calculates the final result internally and returns it once done.

    Note: In the iterative example, a lot of time is wasted in the decimal to byte-array conversion stage, which occurs between every calculation to display the result. This is obvious when comparing, for example, 500! via the two methods.

    « Back to Examples