Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread operator vs apply (random json objects 5000)
(version: 1)
Compare the differing ways you can call a function with arbitrary arguments dynamically
Comparing performance of:
spread vs apply
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function test() { console.log(arguments[arguments.length - 1]); } var using = (new Array(5000)).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" }));
Tests:
spread
test(...using);
apply
test.apply(test, using)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread
apply
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread
80541.6 Ops/sec
apply
77283.1 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark in question is designed to compare the performance of two different JavaScript methods for invoking a function with a variable number of arguments: the spread operator and the `apply` method. Let's delve into the specifics. ### Benchmark Components 1. **Benchmark Definition**: - **Test Functions**: - `test(...using);` - This represents the spread operator, which takes an iterable (in this case, the `using` array of JSON objects) and expands it into individual arguments for the `test` function. - `test.apply(test, using)` - This uses the `apply` method of a function, where you invoke `test` with a specific `this` context (which can be set to `test` itself, in this case) and an array of arguments, which are provided via the `using` array. 2. **Data Used**: - The benchmark uses a collection of 5000 randomly generated JSON objects; each has various fields like `_id`, `index`, `guid`, `balance`, etc. The main purpose here is to ensure that enough data is supplied to effectively test the performance of the function call method. ### Performance Results - **Spread Operator**: - Executions Per Second: **80,541.55** - **Apply Method**: - Executions Per Second: **77,283.09** From the results, the spread operator outperforms the apply method in terms of executions per second. ### Pros and Cons #### **Spread Operator** - **Pros**: - More readable and concise syntax. - Offers improved performance in modern JavaScript engines, as evidenced by the benchmark results. - **Cons**: - Variable argument length could lead to memory issues with very large arrays. #### **Apply Method** - **Pros**: - Provides a way to specify the `this` context explicitly; helpful in certain scenarios where context matters. - Known and familiar to many developers who have used JavaScript for a longer time. - **Cons**: - More verbose and less straightforward than using the spread operator. - May also be less performant compared to the spread operator. ### Other Considerations 1. **Browser Support**: The spread operator is well-supported in modern browsers, making it a go-to choice when dealing with function arguments in ES6 and newer code. However, older environments that do not support ES6 will necessitate the use of methods like `apply`. 2. **Use Cases**: In practice, choosing between these two methods may depend on your codebase, developer familiarity, and concerns over performance or readability. When handling a fixed number of arguments or dealing with legacy code, `apply` might still be relevant. ### Alternatives - **Arguments Object**: JavaScript functions can access the `arguments` object – an array-like object that holds all provided arguments. This allows for a flexible function signature but lacks the benefit of modern syntax and array methods. - **Rest Parameters**: A modern equivalent to handling variable arguments, this syntax (`function test(...args)`) is considered to be a friendlier and more powerful alternative in contexts where you need to handle multiple parameters. In summary, the benchmark effectively tests the performance differences between these two methods of function invocation, showcasing the efficiency of the spread operator in modern JS programming, while also providing insights into the pros and cons of each approach for different use cases.
Related benchmarks:
fork Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings.
Lodash.isEqual vs JSON.stringify big object
hash-sum (2.0) vs object-hash (3.0)
JSON.stringify vs Text (huge json dummy)
lodash latest clonedeep vs json.parse(stringify()) vs recursivecopy
Lodash.isEqual vs JSON.stringify Equality Comparison for Deep Array of Objects
lodash merge vs object.assign vs spread 1
Spread operator vs apply (random json objects)
Concat vs push(...) for large arrays (500 up to 50000)
Comments
Confirm delete:
Do you really want to delete benchmark?