Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Memoization functions
(version: 0)
Test different memoization functions
Comparing performance of:
Memoize vs Memoize without closure
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
//------------------ SETUP function memoizeWithoutClosure(fn) { const memoizedFunction = () => { if (memoizedFunction.cache === undefined) { memoizedFunction.cache = fn(); } return memoizedFunction.cache; }; return memoizedFunction; } function memoize(fn) { let cachedResult; return () => { if (cachedResult === undefined) { cachedResult = fn(); } return cachedResult; }; } var funcs = [...Array(5000).keys()].map(_ => (() => 'ashkjsaskhkajhdkajhdk')); //----------- MEMOIZE WIthour Closure const memoizedFuncs = funcs.map(fn => memoizeWithoutClosure(fn)); memoizedFuncs.forEach(fn => fn()); memoizedFuncs.forEach(fn => fn());
Tests:
Memoize
//------------ MEMOIZE const memoizedFuncs = funcs.map(fn => memoize(fn)); memoizedFuncs.forEach(fn => fn()); memoizedFuncs.forEach(fn => fn());
Memoize without closure
//----------- MEMOIZE without Closure const memoizedFuncs = funcs.map(fn => memoizeWithoutClosure(fn)); memoizedFuncs.forEach(fn => fn()); memoizedFuncs.forEach(fn => fn());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Memoize
Memoize without closure
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 16 on iOS 16.3.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Memoize
302.6 Ops/sec
Memoize without closure
291.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring performance of JavaScript microbenchmarks is an essential task to ensure optimal performance and scalability of web applications. **What's being tested?** The provided JSON represents two benchmark tests: "Memoization functions". The test focuses on comparing the performance of two different memoization techniques in JavaScript: 1. **Traditional Memoization (with closure)**: This approach uses a closure to cache the results of a function call, ensuring that subsequent calls with the same input return the cached result instead of recalculating it. 2. **Memoization without Closure**: This approach achieves similar caching as the traditional memoization method but without using closures. Instead, it relies on variables to store the cached results. **Options being compared** The two options are compared in terms of their execution speed and performance. **Pros and Cons:** * **Traditional Memoization (with closure)**: + Pros: - More efficient use of memory - Can handle complex caching logic easily + Cons: - Requires the creation of a new function scope (closure) for caching, which can lead to higher memory allocation and garbage collection overhead. * **Memoization without Closure**: + Pros: - Less memory usage compared to traditional memoization - Still achieves decent performance with careful implementation + Cons: - Requires more manual management of caching variables, which can be error-prone **Library and purpose** In the provided benchmark code, there is no explicit library mentioned. However, the `memoizeWithoutClosure` function relies on the `var` keyword to create a variable scope for caching, which is not specific to any particular library. **Special JS feature or syntax** There are no special JavaScript features or syntax used in this benchmark that would be unique to recent versions of the language. The code focuses on basic JavaScript concepts and implementation details. **Other alternatives** If you were considering alternative memoization techniques for your use case, some options might include: * **Proxy-based caching**: Using JavaScript's built-in `Proxy` object or libraries like `fast-proxies` to create a proxy that caches results. * **WeakMap-based caching**: Utilizing JavaScript's `WeakMap` data structure to store cached values with weak references, ensuring memory efficiency and minimal garbage collection overhead. When selecting an alternative memoization approach, consider factors such as performance requirements, complexity of your use case, and the trade-offs between memory usage and execution speed.
Related benchmarks:
Direct call vs bind vs call vs apply spred
Object.values vs for in loop vs for loop
Array loop vs foreach vs map saved
Array loop vs foreach vs map into array
Array loop vs foreach vs map into array 2
Comments
Confirm delete:
Do you really want to delete benchmark?