Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
splice vs =
(version: 0)
100k list splice and shift win, they mutate list slice loose, it creates a copy of list 7.5x slower
Comparing performance of:
= vs splice
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var list = []; for (var i = 0; i < 1000 * 1000; i++) { list.push(i); }
Tests:
=
list = [];
splice
list.splice(0, list.length);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
=
splice
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, the options being compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark definition represents two different ways to create an empty array in JavaScript: using the assignment operator (=) and the `splice` method. ```json "Name": "splice vs =", "Description": "100k list\nsplice and shift win, they mutate list\nslice loose, it creates a copy of list\n7.5x slower" ``` The description suggests that the `splice` method is generally faster than using the assignment operator for creating an empty array. **Script Preparation Code** This code initializes a large array (`list`) with 1 million elements. ```javascript var list = []; for (var i = 0; i < 1000 * 1000; i++) { list.push(i); } ``` This preparation code is used to create a large array, which will be modified by the `splice` method in one test case and assigned using the assignment operator in the other. **Options being compared** Two options are being compared: 1. **Assignment Operator (=)**: This creates a new variable `list` and assigns it an empty value. ```javascript var list = []; ``` Pros: simple, concise, and widely supported. Cons: can lead to unexpected behavior if not used carefully (e.g., assigning values to existing variables). 2. **Splice Method (`splice`)**: This modifies the original array by removing elements starting from index 0 until the end of the array. ```javascript list.splice(0, list.length); ``` Pros: can be efficient for arrays with a large number of empty slots. Cons: modifies the original array, which might not be desirable in some scenarios; also, it's generally slower than using the assignment operator. **Library and special features** There is no explicit library mentioned in the benchmark definition. However, note that Chrome Edgion (not "Edg/92.0.902.73") uses a custom JavaScript engine that might have different behavior compared to other browsers or engines. No special JavaScript features or syntax are being tested in this benchmark. **Alternative approaches** Other approaches to create an empty array include: * Using `Array.from()` and passing an empty iterable: `var list = Array.from();` * Using the `new` keyword with `Array`: `var list = new Array();` * Using a function that returns an empty array: `function getEmptyList() { return []; } var list = getEmptyList();` These alternatives are not being tested in this benchmark. **Benchmark preparation code for individual test cases** Two individual test cases are provided, each with its own script preparation code: 1. `=` ```javascript var list = []; for (var i = 0; i < 1000 * 1000; i++) { list.push(i); } ``` 2. `splice` ```javascript list.splice(0, list.length); ``` These test cases are likely used to measure the performance of each approach in isolation. **Latest benchmark result** The latest benchmark results show that Chrome 92 (on a desktop platform) executes the assignment operator (`=`) approximately 7.5x faster than the `splice` method, which is consistent with the description provided in the benchmark definition.
Related benchmarks:
slice VS splice (clone) VS shift: who is the fastest to keep constant size
slice VS splice VS shift: who is the fastest to keep constant size (fork no string push)
slice VS splice VS shift: who is the fastest to keep constant size 100
slice VS splice VS shift: smaller list, copy last half of array
Comments
Confirm delete:
Do you really want to delete benchmark?