var func = new Function('a', 'b',' return a + b');
var func2 = ( a, b ) => a+b;
func(1,2)
func2(1,2)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new | |
code |
Test name | Executions per second |
---|---|
new | 10318514.0 Ops/sec |
code | 10305545.0 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what's being tested.
Benchmark Definition:
The benchmark is comparing two approaches:
new Function()
: This creates a new function at runtime using the Function
constructor, which takes a string of arguments as its first parameter.( ) => expression
: This creates an anonymous function that takes variables as input and returns a value.In this specific benchmark, the comparison is between the new Function()
approach and the anonymous arrow function approach for executing a simple arithmetic expression: func(a, b) = a + b
.
Pros and Cons:
new Function()
:( ) => expression
:Library:
There is no explicit library mentioned in the benchmark definition, but it's worth noting that Function
and arrow functions are built-in JavaScript features. The use of new Function()
might be more common in older JavaScript code or when working with specific libraries or frameworks that rely on this syntax.
Special JS Feature/Syntax:
There is no special JavaScript feature or syntax mentioned in the benchmark definition, but it's worth noting that arrow functions were introduced in ECMAScript 2015 (ES6) and have since become a standard part of modern JavaScript.
Benchmark Preparation Code:
The provided code snippet creates two functions:
func
using new Function()
, which takes a string of arguments ('a', 'b'
) and returns the sum of a
and b
.func2
using an anonymous arrow function, which also takes variables a
and b
as input and returns their sum.The benchmark comparison is between these two functions, with each test case executing a different function call with the same arguments (1
, 2
) to measure their performance differences.
Other Alternatives:
For measuring function execution speed, alternatives might include:
performance.now()
, requestAnimationFrame()
) or third-party libraries.