function test() { console.log(arguments[arguments.length - 1]); }
var using = (new Array(200)).fill(null).map((e, i) => ( {
"_id": "6756f95de1bf2a3657bf2262",
"index": 0,
"guid": "bdc23f10-a1ac-47ad-a423-1c498f5d7857",
"isActive": true,
"balance": "$2,288.04",
"picture": "http://placehold.it/32x32",
"age": i,
"eyeColor": "brown",
"name": "Reid Holden",
"gender": "male",
"company": "KAGGLE",
"email": "reidholden@kaggle.com",
"phone": "+1 (844) 427-3173",
"address": "923 Dewitt Avenue, Macdona, Florida, 2333",
"about": "Eu quis amet ipsum id occaecat reprehenderit cillum cupidatat ullamco ad duis. Lorem tempor incididunt culpa dolor labore et officia nulla aliqua et incididunt amet dolor reprehenderit. Ut voluptate est elit quis fugiat ex magna irure eiusmod esse duis sint qui. Exercitation duis duis ullamco est eiusmod sunt eiusmod velit laboris ex elit.\r\n",
"registered": "2017-07-12T07:52:14 -02:00",
"latitude": 49.456753,
"longitude": -37.824429,
"tags": [
"est",
"laborum",
"laborum",
"incididunt",
"adipisicing",
"anim",
"sit"
],
"friends": [
{
"id": 0,
"name": "Gutierrez Collins"
},
{
"id": 1,
"name": "Lisa Richardson"
},
{
"id": 2,
"name": "Wilson Kane"
}
],
"greeting": "Hello, Reid Holden! You have 4 unread messages.",
"favoriteFruit": "strawberry"
}));
test(using);
test.apply(null, using)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread | |
apply |
Test name | Executions per second |
---|---|
spread | 185288.9 Ops/sec |
apply | 175070.3 Ops/sec |
The benchmark defined in the provided JSON focuses on comparing two different ways to invoke a function with a list of arguments in JavaScript: using the spread operator and using the apply
method. This is a common performance consideration for JavaScript developers, particularly when dealing with functions that take a variable number of arguments or function calls involving constructs like arrays or objects.
Spread Operator (test(...using);
):
...
) allows iterable elements (like arrays) to be expanded into individual arguments in function calls. apply
Method (test.apply(null, using)
):
apply
method is a built-in function that calls a function with a given this
value and arguments provided as an array (or array-like object).This benchmark does not utilize any external libraries; it purely uses native JavaScript functionality (the spread operator and .apply()
). The benchmark tests are structured with a set of randomized JSON objects to simulate the dynamic nature of real-world application data.
From the results provided:
apply
version achieved 175,070.34 executions per second.This result indicates that the spread operator performed slightly better in this particular test case.
Function.prototype.call: Another alternative for calling functions with a variable number of arguments is using call
, which functions similarly to apply
, but takes individual arguments rather than an array. This can also be tested for performance comparisons:
test.call(null, ...using); // Using call with spread
Performance Impact: When choosing between these options, it's important to consider the context and the expected argument sizes. For smaller or moderate-sized arrays, the spread operator might be favored for its readability and performance. For very large arrays, testing with the specific JavaScript engine in use and considering memory management could guide the decision.
Code Complexity: Ultimately, the choice may also depend on team standards for code style, maintainability, and browser compatibility requirements. If supporting legacy environments is a priority, using apply
may be the safer, albeit less elegant, fallback.
In summary, the benchmark effectively compares the performance of two JavaScript function-call styles—spread syntax and apply
. Each approach has its unique advantages and trade-offs, particularly concerning legibility and performance in different scenarios, making it essential for developers to consider their application needs and environment when choosing between them.