Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array filtering: (splice + predefined index) vs (filter) - big array
(version: 1)
Comparing performance of:
splice vs filter
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr1 = new Array(10000).fill(0); var arr2 = new Array(10000).fill(0); var indexesToDelete = [3, 10, 40, 90, 150, 500, 1000, 1500, 2000, 2500, 3000, 4000, 5000, 6000, 7000, 8000, 9000]; indexesToDelete.forEach(index => { arr1[index] = 1; arr2[index] = 1; });
Tests:
splice
indexesToDelete.forEach(index => { arr1.splice(index, 1); });
filter
const newArr = arr2.filter(value => value !== 1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
splice
filter
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 benchmark and explain what is being tested. **Benchmark Definition** The benchmark is defined as an array filtering operation, where we need to delete specific elements from an array using two different approaches: 1. `splice`: This method modifies the original array by removing a specified number of elements at a given index. 2. `filter`: This method creates a new array with all elements that pass a test implemented by a provided function. **Options Compared** The benchmark compares the performance of these two approaches on a large array (10,000 elements) and 14 specific indexes that need to be deleted. **Pros and Cons** * **splice**: + Pros: Modifies the original array in-place, which can lead to better performance for certain use cases. + Cons: Can be slower than `filter` for very large arrays, as it requires shifting all elements after the removed index. * **filter**: + Pros: Creates a new array without modifying the original one, which can be faster and more memory-efficient for very large datasets. + Cons: Requires extra memory to store the new array, which can be a limitation. **Library** None of the benchmark code uses any external libraries. The `Array.prototype.splice()` method is a built-in JavaScript function. **Special JS Feature or Syntax** The benchmark does not use any special JavaScript features or syntax that would require explanation beyond the scope of this answer. **Other Alternatives** If you need to delete specific elements from an array, you could also consider using: * `slice()`: Similar to `splice`, but returns a new array with the removed elements. * `indexOf()` and `splice()`: These methods can be used together to remove multiple elements at once. * `reduce()`: This method can be used to create a new array by applying a reduction function to each element. Keep in mind that the performance differences between these approaches may vary depending on the specific use case and array size. **Benchmark Preparation Code** The script preparation code creates two large arrays (`arr1` and `arr2`) filled with zeros, and defines an array of indexes to delete from each array. It then iterates through this array and sets each element at the specified index to 1 using both the `splice()` and `filter()` methods. **Individual Test Cases** The benchmark has two individual test cases: * "splice" * "filter" These test cases execute the corresponding benchmark definition code and measure their performance.
Related benchmarks:
comparing Array.from copy and then splice with filter method
splice(indexof()) vs filter
Filter vs FindIndex splice
Deleting using .splice vs .filter
filter vs findIndex & splice (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?