Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Memoized Factorial
(version: 0)
Comparing performance of:
Factorial memoized vs Factorial plain
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const memoize = fn => { const cache = {}; return (...args) => { argStr = JSON.stringify(args); cache[argStr] = cache[argStr] || fn(args); return cache[argStr]; } } const factorial = memoize((n) => { if (n <= 1) return 1; return n * factorial(n - 1); })
Tests:
Factorial memoized
const memoize = fn => { const cache = {}; return (...args) => { argStr = JSON.stringify(args); cache[argStr] = cache[argStr] || fn(args); return cache[argStr]; } } const factorial = memoize((n) => { if (n <= 1) return 1; return n * factorial(n - 1); }); factorial(10);
Factorial plain
const factorial =(n) => { if (n <= 1) return 1; return n * factorial(n - 1); }; factorial(10)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Factorial memoized
Factorial plain
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Factorial memoized
74246.6 Ops/sec
Factorial plain
11584203.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared options, pros/cons of each approach, and other considerations. **Benchmark Definition** The benchmark measures the performance difference between two approaches to calculating the factorial of a number: 1. **Memoized Factorial**: This approach uses a caching mechanism to store previously computed values of the factorial function. When the same input is provided again, the cached value is returned instead of recomputing it. 2. **Plain Factorial**: This approach does not use any caching or optimization techniques. **Script Preparation Code** The script preparation code defines two functions: * `memoize`: A higher-order function that takes another function as an argument and returns a new function with memoization enabled. * `factorial`: The factorial function being benchmarked, which is either memoized (in the first test case) or plain (in the second test case). **Html Preparation Code** The html preparation code is empty, indicating that no specific HTML setup is required for this benchmark. **Individual Test Cases** There are two individual test cases: 1. **Factorial memoized**: This test case uses the `memoize` function to enable memoization for the `factorial` function. 2. **Factorial plain**: This test case does not use any optimization techniques, resulting in a plain implementation of the factorial function. **Comparison** The benchmark compares the performance of these two approaches: * **Memoized Factorial** (test case 1): Uses caching to store previously computed values, reducing redundant computations. * **Plain Factorial** (test case 2): Does not use any optimization techniques, resulting in more computationally expensive code. **Pros and Cons** **Memoized Factorial:** Pros: * Reduces computational overhead by avoiding redundant calculations. * Can improve performance for large input values or repeated calls with the same input. Cons: * Requires additional memory to store cached values. * May introduce complexity due to the caching mechanism. **Plain Factorial:** Pros: * Simple and easy to understand implementation. * Does not require additional memory or complex logic. Cons: * Can be computationally expensive for large input values or repeated calls with the same input. **Other Considerations** * The benchmark does not account for other factors that may affect performance, such as garbage collection, memory allocation, or other concurrent processes. * The `memoize` function uses a simple caching mechanism, which might not be suitable for all use cases (e.g., cache invalidation, cache size limits). * The benchmark assumes that the input values are integers; if non-integer inputs are expected, additional considerations would be necessary. **Alternatives** Other approaches to implementing factorial calculations could include: * Using a lookup table or precomputed values for small input values. * Employing iterative or recursive algorithms with optimizations (e.g., memoization, dynamic programming). * Utilizing specialized libraries or functions optimized for performance (e.g., `Math.factorial` in modern browsers). These alternatives would require additional testing and evaluation to determine their suitability for specific use cases.
Related benchmarks:
JSON Stringify Speed Test
Date JsonReviver
JSON stringify vs array join 2
JSON Stringify Speed Test2
json stringify vs object tostring number thresholds
Comments
Confirm delete:
Do you really want to delete benchmark?