Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reassigning an object with larger arrray
(version: 0)
Comparing performance of:
index vs map
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [ {'id': 1, 'name': 'Item 1'}, {'id': 2, 'name': 'Item 2'}, {'id': 3, 'name': 'Item 3'}, {'id': 4, 'name': 'Item 4'}, {'id': 5, 'name': 'Item 5'}, {'id': 6, 'name': 'Item 6'}, {'id': 7, 'name': 'Item 7'}, {'id': 8, 'name': 'Item 8'}, {'id': 9, 'name': 'Item 9'}, {'id': 10, 'name': 'Item 10'}, {'id': 11, 'name': 'Item 11'}, {'id': 12, 'name': 'Item 12'}, {'id': 13, 'name': 'Item 13'}, {'id': 14, 'name': 'Item 14'}, {'id': 15, 'name': 'Item 15'}, {'id': 16, 'name': 'Item 16'}, {'id': 17, 'name': 'Item 17'}, {'id': 1, 'name': 'Item 1'}, {'id': 65, 'name': 'Item 2'}, {'id': 3, 'name': 'Item 3'}, {'id': 4, 'name': 'Item 4'}, {'id': 5, 'name': 'Item 5'}, {'id': 6, 'name': 'Item 6'}, {'id': 7, 'name': 'Item 7'}, {'id': 8, 'name': 'Item 8'}, {'id': 9, 'name': 'Item 9'}, {'id': 10, 'name': 'Item 10'}, {'id': 11, 'name': 'Item 11'}, {'id': 12, 'name': 'Item 12'}, {'id': 13, 'name': 'Item 13'}, {'id': 14, 'name': 'Item 14'}, {'id': 15, 'name': 'Item 15'}, {'id': 16, 'name': 'Item 16'}, {'id': 17, 'name': 'Item 17'}, {'id': 1, 'name': 'Item 1'}, {'id': 28, 'name': 'Item 2'}, {'id': 3, 'name': 'Item 3'}, {'id': 4, 'name': 'Item 4'}, {'id': 5, 'name': 'Item 5'}, {'id': 6, 'name': 'Item 6'}, {'id': 7, 'name': 'Item 7'}, {'id': 8, 'name': 'Item 8'}, {'id': 9, 'name': 'Item 9'}, {'id': 10, 'name': 'Item 10'}, {'id': 11, 'name': 'Item 11'}, {'id': 12, 'name': 'Item 12'}, {'id': 13, 'name': 'Item 13'}, {'id': 14, 'name': 'Item 14'}, {'id': 15, 'name': 'Item 15'}, {'id': 16, 'name': 'Item 16'}, {'id': 17, 'name': 'Item 17'}, {'id': 1, 'name': 'Item 1'}, {'id': 65, 'name': 'Item 2'}, {'id': 3, 'name': 'Item 3'}, {'id': 4, 'name': 'Item 4'}, {'id': 5, 'name': 'Item 5'}, {'id': 6, 'name': 'Item 6'}, {'id': 7, 'name': 'Item 7'}, {'id': 8, 'name': 'Item 8'}, {'id': 9, 'name': 'Item 9'}, {'id': 10, 'name': 'Item 10'}, {'id': 11, 'name': 'Item 11'}, {'id': 12, 'name': 'Item 12'}, {'id': 13, 'name': 'Item 13'}, {'id': 14, 'name': 'Item 14'}, {'id': 15, 'name': 'Item 15'}, {'id': 16, 'name': 'Item 16'}, {'id': 17, 'name': 'Item 17'}, ]; var newObject = { id: 2, name: 'Updated Item 2' };
Tests:
index
var index = array.findIndex(item => item.id === newObject.id); var updatedArray; if (index !== -1) { updatedArray = [ ...array.slice(0, index), // Elements before the updated item newObject, // The updated item ...array.slice(index + 1) // Elements after the updated item ]; } else { // If the item isn't found, the array remains unchanged updatedArray = array; }
map
var arr = array.map(item => { if (item.id === newObject.id) { return newObject; } return item; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
index
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
index
2794693.0 Ops/sec
map
462592.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark definition and test cases. **Benchmark Definition** The `reassigning an object with larger array` benchmark measures how fast JavaScript can reassign an object when its inner array is longer than the number of elements in the original array. The benchmark creates a large array, assigns it to a variable, and then updates it by assigning a new object that has one property (`id`) matching another property (`name`) from the original array. **Options Compared** The benchmark compares two approaches: 1. `index`: Uses `Array.prototype.findIndex` to find the index of the updated item in the original array. If found, it creates a new array with the updated item at that position. If not found, the original array remains unchanged. 2. `map`: Uses `Array.prototype.map` to create a new array with all elements transformed according to a provided function. In this case, if an element's `id` matches the updated object's `id`, it returns the updated object; otherwise, it returns the original element. **Individual Test Cases** The benchmark defines two test cases: 1. `index`: Tests the performance of finding and updating an item in the array using `Array.prototype.findIndex`. 2. `map`: Tests the performance of creating a new array with all elements transformed using `Array.prototype.map`. **Benchmark Results** The latest benchmark results show the execution frequency per second for each test case on Chrome 120, running on a Mac OS X 10.15.7 system. 1. `index`: Executes approximately 2.79 million times per second. 2. `map`: Executes approximately 462,592 times per second. In summary, using `Array.prototype.findIndex` is significantly faster than using `Array.prototype.map` for this specific use case, likely due to the optimized implementation of `findIndex` and the fact that it only scans the array once to find the index.
Related benchmarks:
Find item in large array - Fork
lodash_array_objects
lodash_array_objects_2
Test-BC
Comments
Confirm delete:
Do you really want to delete benchmark?