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 | 88118.4 Ops/sec |
Lazy with fetch call | 89472.1 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark created on MeasureThat.net. The benchmark compares the efficiency of two lazy implementation approaches: mkLazyTest
and mkLazyCall
. These functions are used to create lazy implementations that replace the original function's thunk
with a cached version.
Options Compared
Two options are compared:
mkLazyTest
: This approach caches the result of the original function's thunk
on the first call and returns the cached value for subsequent calls.mkLazyCall
: This approach creates a new get
function that returns the cached value when called, and assigns this get
function to itself recursively.Pros and Cons
mkLazyTest
:value
has been set).mkLazyCall
:mkLazyTest
, as it avoids the existence test.Library Usage
The provided benchmark uses a custom library (cfn
) that defines a function cfn
with the signature (v)
-> (function())
. The cfn
function returns an anonymous function that always returns the original value. This function is used as the input to both mkLazyTest
and mkLazyCall
.
Special JavaScript Features or Syntax
None.
Other Considerations
Alternatives
For similar benchmarks, you can explore other options:
mkLazyCallV2
: A revised version of mkLazyCall
that avoids recursion-based implementation and instead uses a simple lookup table to store cached values.fibonacci-batch
: A benchmark for measuring the performance of batch-based Fibonacci implementations, which can be optimized for different use cases (e.g., generating small vs large Fibonacci numbers).array-parallelization
: A benchmark comparing parallelization strategies for array operations, such as map(), reduce(), and filter().These alternatives focus on specific aspects of JavaScript performance, allowing developers to compare and optimize their own implementations or explore different optimization techniques.