Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reselect defaultMemoize
(version: 9)
Comparing performance of:
Original defaultMemoize vs Optimized defaultMemoize vs No memoization
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function defaultEqualityCheck(a, b) { return a === b } function defaultMemoize_original(func, equalityCheck = defaultEqualityCheck) { let lastArgs = null let lastResult = null return (...args) => { if ( lastArgs === null || lastArgs.length !== args.length || !args.every((value, index) => equalityCheck(value, lastArgs[index])) ) { lastResult = func(...args) } lastArgs = args return lastResult } } function defaultMemoize_optimized(func, equalityCheck = defaultEqualityCheck) { const isEqual = (value, index) => equalityCheck(value, lastArgs[index]) let lastArgs = null let lastResult = null return (...args) => { if ( lastArgs === null || lastArgs.length !== args.length || !args.every(isEqual) ) { lastResult = func(...args) } lastArgs = args return lastResult } } function combine(a, b) { return { combined: a + b }; } var combine_original = defaultMemoize_original(combine); var combine_optimized = defaultMemoize_optimized(combine); var NUM_REPEAT = 50000;
Tests:
Original defaultMemoize
let result1 = 0; for (let i = 0; i < NUM_REPEAT; ++i) result1 += combine_original(1, 2).combined;
Optimized defaultMemoize
let result2 = 0; for (let i = 0; i < NUM_REPEAT; ++i) result2 += combine_optimized(1, 2).combined;
No memoization
let result3 = 0; for (let i = 0; i < NUM_REPEAT; ++i) result3 += combine(1, 2).combined;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Original defaultMemoize
Optimized defaultMemoize
No memoization
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Original defaultMemoize
284.4 Ops/sec
Optimized defaultMemoize
292.6 Ops/sec
No memoization
321.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. The test cases are designed to compare the performance of three different versions of a memoization function, which is a technique used to optimize repetitive computations by caching their results. **Memoization basics** In this context, memoization is a technique where the result of an expensive computation (in this case, a simple addition) is cached and reused when the same inputs occur again. This avoids redundant calculations, reducing the overall execution time. The benchmark defines three different versions of a `defaultMemoize` function: 1. **Original defaultMemoize**: This version uses a simple approach to memoization, where it checks if the arguments are the same as in the previous call and returns the cached result if they are. 2. **Optimized defaultMemoize**: This version is optimized for performance by using a constant function `isEqual` that compares values at specific indices (in this case, the first value). It still uses the same basic approach to memoization but with some optimizations. **Test cases** There are three test cases: 1. **Original defaultMemoize**: This test case runs a simple addition operation (`combine(1, 2)`) repeated `NUM_REPEAT` times and measures the execution time. 2. **Optimized defaultMemoize**: Similar to the first test case, but uses the optimized version of `defaultMemoize`. 3. **No memoization**: This test case also runs the same simple addition operation repeatedly, without any memoization. **Pros and cons** Here are some pros and cons of each approach: * **Original defaultMemoize**: + Pros: Easy to implement and understand. + Cons: May have higher execution time due to repeated checks for equality. * **Optimized defaultMemoize**: + Pros: Optimized performance with fewer checks for equality. + Cons: Requires a separate constant function `isEqual` which may add complexity. Other considerations: * The test cases use a simple addition operation (`combine(1, 2)`) to demonstrate the memoization technique. In real-world scenarios, this might not be as representative of actual usage. * The benchmark runs on Chrome 121 on a Mac with Intel CPU, which may affect the results due to differences in hardware and browser performance. **Alternatives** If you were to modify or extend these benchmarks, some alternatives to consider: * Use different types of computations (e.g., more complex functions, loops, or data structures). * Experiment with different browsers, operating systems, or devices. * Add more test cases to cover various edge cases or scenarios. * Investigate other optimization techniques for memoization, such as using a cache map or a library like `lru-cache`. These considerations can help refine the benchmark and provide a more comprehensive understanding of the performance differences between these memoization approaches.
Related benchmarks:
Typeof GlobalThis Check
Option chaining and typeof
Option chaining and typeof 2
optional chain vs typeof
lose comparison and type coercion vs strict 2 x strict comparison
Comments
Confirm delete:
Do you really want to delete benchmark?