Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Custom splice based array filter
(version: 3)
Comparing performance of:
custom array filter vs custom array filter with bigger array vs Array.filter
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function arrayFilter(data, filterFn) { const length = data.length; // we go from back to front to avoid index problems (elements change on the same index) and length changes for (let i = length - 1; i >= 0; i--) { if (filterFn(data[i]) === false) { data.splice(i, 1); } } return data; }
Tests:
custom array filter
const testData = []; for(let i=0; i<100; i++) { testData.push(i % 2); } arrayFilter(testData, i=>!i);
custom array filter with bigger array
const testData = []; for(let i=0; i<100; i++) { testData.push(i % 2); } for(let i=0; i<100; i++) { testData.push(0); } arrayFilter(testData, i=>!i);
Array.filter
const testData = []; for(let i=0; i<100; i++) { testData.push(i % 2); } testData.filter(i => !i);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
custom array filter
custom array filter with bigger array
Array.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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark measures the performance of different approaches for filtering an array. **What is tested?** Three test cases are compared: 1. **Custom splice based array filter**: This implementation uses a custom function `arrayFilter` to filter the array by iterating over it from back to front (i.e., from the last element to the first) and removing elements that do not match the filter condition. 2. **Array.filter**: This is the built-in JavaScript method for filtering arrays. 3. **Custom array filter with bigger array**: This test case uses a larger array size than the previous two, to stress-test the performance of the custom filter implementation. **Options compared** The following options are compared: * Custom splice based array filter * Array.filter **Pros and Cons of each approach:** 1. **Custom splice based array filter**: * Pros: + Can be optimized for specific use cases or array sizes. + Might be faster for very large arrays due to the "back-to-front" iteration strategy. * Cons: + More complex and harder to understand than the built-in `Array.filter`. + May have performance overhead due to the custom implementation. 2. **Array.filter**: * Pros: + Much simpler and easier to understand than the custom implementation. + Part of the standard JavaScript library, so well-optimized and maintained. * Cons: + May be slower for very large arrays due to the overhead of creating a new array. + Less control over the iteration strategy. **Library usage** The `arrayFilter` function uses a custom implementation. There is no external library used in this benchmark. **Special JavaScript features or syntax** None mentioned in the provided code snippets. **Other alternatives** If you want to explore other filtering options, you might consider: * Using a different data structure, such as a Set or Map. * Employing a more efficient algorithm, like a Boyer-Moore or Knuth-Morris-Pratt approach for matching. * Utilizing parallel processing or multi-threading techniques to accelerate the filtering process. Note that these alternatives would likely require significant changes to the benchmark code and may not be directly comparable to the original custom splice based array filter implementation.
Related benchmarks:
comparing Array.from copy and then splice with filter method
comparing Array.from copy and then splice with filter method to variable
Lodash filter vs splice removing item from array
filter vs splice 2
Comments
Confirm delete:
Do you really want to delete benchmark?