Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Remove from array
(version: 0)
Remove from array
Comparing performance of:
Default vs Reverse vs Fix
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var letters = ["A", "B", "C", "D", "E", "F"];
Tests:
Default
var copy = letters.slice(); var length = copy.length; for (var i = 0; i < length; i++) { if (copy[i] === "B" || copy[i] === "C") { letters.splice(letters.indexOf(copy[i]), 1); } }
Reverse
var copy = letters.slice(); var length = copy.length; for (var i = length - 1; i >= 0; i--) { if (copy[i] === "B" || copy[i] === "C") { letters.splice(i, 1); } }
Fix
var copy = letters.slice(); var length = copy.length; for (var i = 0; i < length; i++) { if (copy[i] === "B" || copy[i] === "C") { letters.splice(i, 1); i--; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Default
Reverse
Fix
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 benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is defined in the JSON object: ``` { "Name": "Remove from array", "Description": "Remove from array", "Script Preparation Code": "var letters = [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\"];", "Html Preparation Code": null } ``` This benchmark tests the performance of removing elements from an array in JavaScript. **Test Cases** There are three test cases: ```json [ { "Benchmark Definition": "var copy = letters.slice();\r\nvar length = copy.length;\r\n\r\nfor (var i = 0; i < length; i++)\r\n{\r\n if (copy[i] === \"B\" || copy[i] === \"C\")\r\n {\r\n letters.splice(letters.indexOf(copy[i]), 1);\r\n }\r\n}", "Test Name": "Default" }, { "Benchmark Definition": "var copy = letters.slice();\r\nvar length = copy.length;\r\n\r\nfor (var i = length - 1; i >= 0; i--)\r\n{\r\n if (copy[i] === \"B\" || copy[i] === \"C\")\r\n {\r\n letters.splice(i, 1);\r\n }\r\n}", "Test Name": "Reverse" }, { "Benchmark Definition": "var copy = letters.slice();\r\nvar length = copy.length;\r\n\r\nfor (var i = 0; i < length; i++)\r\n{\r\n if (copy[i] === \"B\" || copy[i] === \"C\")\r\n {\r\n letters.splice(i, 1);\r\n i--;\r\n }\r\n}", "Test Name": "Fix" } ] ``` Each test case uses a different approach to remove elements from the `letters` array: 1. **Default**: Iterates through the original array using a traditional for loop. 2. **Reverse**: Iterates through the array in reverse order, starting from the end and moving backwards to the beginning. 3. **Fix**: A variation of the default approach that modifies the loop index (`i--`) to skip elements. **Library** None, this benchmark doesn't use any external libraries. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in these test cases. They only rely on standard JavaScript features like arrays, loops, and the `splice()` method. **Benchmark Results** The latest benchmark results show: * **Default**: 2417688.25 executions per second * **Reverse**: 2388430.0 executions per second * **Fix**: 2386369.0 executions per second These results suggest that the traditional for loop approach (Default) is the fastest, followed by the reverse iteration approach (Reverse), and then the modified loop index approach (Fix). **Alternatives** Other alternatives for removing elements from an array in JavaScript include: * Using `filter()` method: `letters.filter(letter => letter !== 'B' && letter !== 'C');` * Using `map()` method with a callback function: `letters.map(letter => { if (letter === 'B' || letter === 'C') return null; return letter; }).flat();` * Using `forEach()` method with a callback function: `letters.forEach(letter => { if (letter === 'B' || letter === 'C') letters.splice(letters.indexOf(letter), 1); });` These alternatives might have different performance characteristics compared to the original test cases, but they can provide an alternative way to achieve the same result.
Related benchmarks:
Remove from array
Remove from array
Remove from array
Remove from array
Comments
Confirm delete:
Do you really want to delete benchmark?