var notDefined;
var defined = 1;
var emptyFunc = function() {};
var trueFunc = function() { return true; }
var undefinedFunc = function() { return undefined; }
var nullFunc = function() { return null; }
var notDefinedFunc = function() { return notDefined; }
var definedFunc = function() { return defined; }
var staticFunc = function() { return 1; }
var localUndefinedFunc = (function() {
var notDefinedAgain;
return function() {
return notDefinedAgain;
}
})();
emptyFunc();
trueFunc();
undefinedFunc();
nullFunc();
notDefinedFunc();
definedFunc();
staticFunc();
localUndefinedFunc();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
emptyFunc | |
trueFunc | |
undefinedFunc | |
nullFunc | |
notDefinedFunc | |
definedFunc | |
staticFunc | |
localUndefinedFunc |
Test name | Executions per second |
---|---|
emptyFunc | 10841400.0 Ops/sec |
trueFunc | 10722474.0 Ops/sec |
undefinedFunc | 3680793.8 Ops/sec |
nullFunc | 10700489.0 Ops/sec |
notDefinedFunc | 5760560.5 Ops/sec |
definedFunc | 4178719.8 Ops/sec |
staticFunc | 10651853.0 Ops/sec |
localUndefinedFunc | 10714090.0 Ops/sec |
Measuring the performance of JavaScript functions is crucial for optimizing code and identifying potential bottlenecks.
The provided benchmark definition json represents a test case that measures the execution time of various JavaScript functions, specifically those that return true, undefined, null, or defined values. The script preparation code includes several function definitions:
emptyFunc
: Returns no value (undefined).trueFunc
: Always returns true
.undefinedFunc
: Returns undefined
.nullFunc
: Always returns null
.notDefinedFunc
: Returns the undefined variable notDefined
.definedFunc
: Returns the defined variable defined
(which is assigned the value 1).staticFunc
: Always returns a fixed value, 1.localUndefinedFunc
: A self-invoking anonymous function that returns the undefined variable notDefinedAgain
.Now, let's discuss the options being compared:
Option 1: Direct Function Call
In this approach, each function is called directly without any modifications. This option tests how fast the JavaScript engine can execute a simple function call.
Pros: Simple and straightforward. Cons: May not accurately represent real-world scenarios where functions are often used in loops or nested calls.
Option 2: Modified Functions (e.g., emptyFunc() = () => {}
)
In this approach, each function is modified to return no value by using an immediately invoked function expression (IIFE). This option tests how fast the JavaScript engine can execute a function that returns nothing.
Pros: More accurate representation of real-world scenarios where functions are used as callbacks or event handlers. Cons: May add overhead due to the IIFE syntax.
Option 3: Self-Invoking Anonymous Functions
In this approach, each function is wrapped in a self-invoking anonymous function (e.g., localUndefinedFunc() = (function() { ... })()
). This option tests how fast the JavaScript engine can execute a function that returns undefined or null.
Pros: More accurate representation of real-world scenarios where functions are used as callbacks or event handlers. Cons: May add overhead due to the self-invoking syntax.
The benchmark result shows that emptyFunc
and trueFunc
are executed faster than the other functions, which is expected since they return no value or a fixed value. The slower execution times of undefinedFunc
, nullFunc
, notDefinedFunc
, and definedFunc
suggest that these functions may be causing some overhead due to their complex behavior.
Other considerations:
Alternative benchmarks: