var msg = "test";
var say = () => {
console.log(msg);
}
say();
this.msg = "test";
var say = () => console.log(this.msg);
say();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Closure | |
Arrow function |
Test name | Executions per second |
---|---|
Closure | 69568.5 Ops/sec |
Arrow function | 68843.7 Ops/sec |
I'd be happy to explain the benchmark being tested on MeasureThat.net.
Benchmark Definition
The benchmark is testing two different approaches in JavaScript: No return vs implicit void return. The script preparation code is empty, indicating that no additional setup or initialization code is needed for the test.
Options Compared
Two options are compared:
say()
returns undefined
(or implicitly, since there's no explicit return statement). This means that when you call say()
, it simply executes and does not provide a value that can be retrieved.say()
returns void
, meaning that it doesn't explicitly return any value. However, due to how JavaScript handles functions with no explicit return statements, this also leads to an implicit return of undefined
. This difference might seem minor, but as we'll see later, it can lead to performance variations.Pros and Cons
No Return (Implicit Void Return):
undefined
, this might not be the most meaningful distinction for performance testing.Options Comparison:
Library and Special Features
There are no libraries used in this benchmark.
Special JS Feature or Syntax
The test case uses Arrows functions, which were introduced in ECMAScript 2015. Arrows functions provide a concise syntax for defining small, one-time use functions without declaring them as full-fledged functions using the function
keyword. This syntax has both advantages and disadvantages compared to traditional function definitions:
Other Alternatives
If the benchmark were testing for performance differences in other JavaScript implementation details or features, alternatives might include:
However, for this specific benchmark focusing on the distinction between no return statements versus implicit void returns in JavaScript functions, the provided setup is well-suited for the analysis at hand.