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;
for (i = 0; i < iterations ; i++) {
lz1();
}
for (i = 0; i < iterations ; i++) {
lz2();
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lazy with existence test | |
Lazy with fetch call |
Test name | Executions per second |
---|---|
Lazy with existence test | 38811.3 Ops/sec |
Lazy with fetch call | 38988.2 Ops/sec |
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:
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.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
mkLazyTest(fn)
):mkLazyCall(fn)
):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:
@lazy
)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.