Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deletion: splice vs filter vs loop
(version: 0)
Comparing performance of:
Splice vs Set-Filter vs Set-Loop
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var people = [{ name: 'juan', age: 18 }, { name: 'maria', age: 25 }, { name: 'luis', age: 32 }, { name: 'ana', age: 22 }, { name: 'pedro', age: 45 }, { name: 'carla', age: 28 }, { name: 'miguel', age: 34 }, { name: 'sofia', age: 29 }, { name: 'antonio', age: 60 }, { name: 'beatriz', age: 19 }, { name: 'fernando', age: 37 }, { name: 'valentina', age: 33 }, { name: 'ricardo', age: 48 }, { name: 'isabel', age: 26 }, { name: 'pablo', age: 40 }, { name: 'sara', age: 21 }, { name: 'alberto', age: 50 }, { name: 'juliana', age: 27 }, { name: 'rafael', age: 36 }, { name: 'natalia', age: 23 }, { name: 'diego', age: 30 }, { name: 'laura', age: 24 }, { name: 'daniel', age: 46 }, { name: 'monica', age: 31 }, { name: 'jorge', age: 38 }, { name: 'camila', age: 20 }, { name: 'linda', age: 41 }, { name: 'roberto', age: 58 }, { name: 'cristina', age: 44 }, { name: 'bruno', age: 39 }, { name: 'catalina', age: 35 }, { name: 'felipe', age: 55 }, { name: 'leticia', age: 42 }, { name: 'victor', age: 43 }, { name: 'marina', age: 47 }, { name: 'iván', age: 49 }, { name: 'angela', age: 53 }, { name: 'martin', age: 56 }, { name: 'estrella', age: 52 }, { name: 'gonzalo', age: 54 }, { name: 'santiago', age: 67 }, { name: 'ines', age: 59 }, { name: 'lucas', age: 62 }, { name: 'paula', age: 64 }, { name: 'javier', age: 70 }, { name: 'alicia', age: 61 }, { name: 'manuel', age: 63 }, { name: 'belen', age: 66 }, { name: 'francisco', age: 65 }, { name: 'jose', age: 68 } ]; var indicesG = [2, 4, 5, 6, 8, 10, 14, 20, 31, 32, 35, 40, 41]; var persistedRequests1 = [...people]; var persistedRequests2 = [...people]; var persistedRequests3 = [...people];
Tests:
Splice
function removeRequestsByIndices(indices) { // Sort indices in descending order to avoid re-indexing issues when splicing indices.sort((a, b) => b - a); // Remove each request at the specified index indices.forEach(index => { if (index >= 0 && index < persistedRequests1.length) { persistedRequests1.splice(index, 1); } }); } removeRequestsByIndices(indicesG);
Set-Filter
function removeRequestsByIndices(indices) { // Create a Set from the indices array for efficient lookup const indicesSet = new Set(indices); // Create a new array excluding elements at the specified indices persistedRequests2 = persistedRequests2.filter((_, index) => !indicesSet.has(index)); } removeRequestsByIndices(indicesG);
Set-Loop
function removeRequestsByIndices(indices) { // Convert indices to a Set for faster lookup const indicesSet = new Set(indices); const newPersistedRequests = []; // Iterate over persistedRequests and build a new array excluding the specified indices for (let i = 0; i < persistedRequests3.length; i++) { if (!indicesSet.has(i)) { newPersistedRequests.push(persistedRequests3[i]); } } // Update the persisted requests persistedRequests3 = newPersistedRequests; } removeRequestsByIndices(indicesG);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Splice
Set-Filter
Set-Loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 SberBrowser/19.0.0.0
Browser/OS:
Chrome 127 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Splice
1485704.1 Ops/sec
Set-Filter
2146673.5 Ops/sec
Set-Loop
1566416.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll answer your question. It appears that you have three benchmark test cases for removing requests by indices from an array of people's names and ages. The tests are labeled as "Set-Filter", "Set-Loop", and "Splice". The latest benchmark result shows the execution speed of each test case, with the fastest execution per second measured at: 1. Set-Filter: 1566416.875 2. Set-Loop: 1485704.125 3. Splice: 2146673.5 These results suggest that using a Set to efficiently look up indices and filtering out elements is the fastest approach, followed by using a loop with conditional statements, and finally, using the Array.prototype.splice() method. Which question would you like me to answer?
Related benchmarks:
lodash_array_objects
lodash_array_objects_2
splice + spread vs filter to remove one item at given index
spread + splice vs filter to remove one item at given index
Comments
Confirm delete:
Do you really want to delete benchmark?