Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lazy test vs call
(version: 0)
Tests if a lazy implementation that uses a test vs one that replaces the thunk function is more efficient
Comparing performance of:
Lazy with existence test vs Lazy with fetch call
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function mkLazyTest(fn) { var thunk = fn, ran = false, value = null; return function() { if (!ran) { value = thunk(); ran = true; } return value; }; } function mkLazyCall(fn) { var thunk = fn; var get = function() { var value = thunk(); get = function() { return value; }; return value; }; return function() { return get(); }; } function cfn(v) { return function() { return v; } } var fn = cfn(1); var lz1 = mkLazyTest(fn); var lz2 = mkLazyCall(fn); var i = 0, iterations = 1000;
Tests:
Lazy with existence test
for (i = 0; i < iterations ; i++) { lz1(); }
Lazy with fetch call
for (i = 0; i < iterations ; i++) { lz2(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lazy with existence test
Lazy with fetch call
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark compares two approaches to implement lazy functions: `mkLazyTest` and `mkLazyCall`. A lazy function is a function that returns a value only when it's actually needed, rather than immediately. This can be useful for improving performance by delaying unnecessary computations. In this case, the test defines three functions: * `cfn`: a simple function that takes an argument `v` and returns another function that simply returns `v`. * `mkLazyTest(fn)`: creates a lazy version of a function `fn`. When called, it checks if the value has already been computed. If not, it computes the value and stores it. On subsequent calls, it simply returns the stored value. * `mkLazyCall(fn)`: creates a lazy version of a function `fn` by replacing the original function with an object that acts as a thunk (a closure). When called, this object executes the original function and caches its result. **Comparison** The test compares the performance of two approaches: 1. **Existence Test (`mkLazyTest(fn)`)**: This approach checks if the value has already been computed by checking a boolean flag `ran`. If not, it computes the value and sets the flag. 2. **Fetch Call (`mkLazyCall(fn)`)**: This approach replaces the original function with an object that acts as a thunk. When called, this object executes the original function and caches its result. The cached result is stored in a variable `value` and returned on subsequent calls. **Pros and Cons** * **Existence Test (`mkLazyTest(fn)`)**: + Pros: simple to implement, easy to understand. + Cons: may lead to unnecessary computations if the value is computed multiple times. * **Fetch Call (`mkLazyCall(fn)`)**: + Pros: avoids unnecessary computations by caching results. + Cons: more complex implementation, requires a thunk object. **Library/Function Descriptions** The `mkLazyTest` and `mkLazyCall` functions are custom implementations for creating lazy functions. They don't rely on any external libraries or frameworks. No special JavaScript features or syntax are used in this benchmark. **Alternative Approaches** Other approaches to implementing lazy functions include: * Using a decorator (e.g., `@lazy`) * Utilizing async functions and await * Implementing a custom cache using a library like Redis or Memcached Keep in mind that these alternatives might have their own trade-offs in terms of performance, complexity, and maintainability. The benchmark results show that the Fetch Call approach (`mkLazyCall(fn)`) outperforms the Existence Test approach (`mkLazyTest(fn)`). This suggests that caching results using a thunk object can lead to better performance.
Related benchmarks:
Return true vs return;
.bind() vs function
Promise vs. Callback
eval vs new Function without cached parsing
just promise vs just callback
Comments
Confirm delete:
Do you really want to delete benchmark?