Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dot vs IndexOf 9
(version: 12)
Comparing performance of:
foundIndex vs Map vs Obj vs Filter
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var length = 100000 var array = new Array(length / 2).fill().map(() => Math.round(Math.random() * length)) array = array.concat(array) array.unshift(length + 1)
Tests:
foundIndex
let temp = [] let foundIndex array.forEach((v) => { foundIndex = temp.indexOf(v) if(foundIndex > -1) temp.splice(foundIndex,1) else temp.push(v) })
Map
let tempMap = new Map() array.forEach((v) => { if(tempMap.has(v)) tempMap.delete(v) else tempMap.set(v, 1) })
Obj
let tempObj = {} array.forEach((v) => { if(tempObj[v]) delete tempObj[v] else tempObj[v]= 1 })
Filter
let temp2 = [] array.forEach((v) => { if(temp2.includes(v)) temp2 = temp2.filter((n) => n != v) else temp2.push(v) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
foundIndex
Map
Obj
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 microbenchmark test case on MeasureThat.net, which compares the performance of different approaches for finding and removing elements from an array. **Options Compared** Four options are compared: 1. **`indexOf()`** method 2. **Filtering using `includes()`** and then filtering out elements using `filter()` 3. **Map object manipulation** 4. **Object property removal using `delete`** These options are tested for their execution speed. **Pros and Cons of Each Approach** * **`indexOf()`**: Pros: Simple, widely supported; Cons: Can be slow for large arrays, may have performance issues with edge cases. * **Filtering using `includes()` and then filtering out elements using `filter()`**: Pros: Simple, effective; Cons: May be slower than other approaches due to the additional operation, can lead to unnecessary work if not used carefully. * **Map object manipulation**: Pros: Efficient for removing properties by key; Cons: Requires an extra data structure (Map), may have performance issues with edge cases or large arrays. * **Object property removal using `delete`**: Pros: Simple, efficient; Cons: Only works on objects, not arrays. **Library Usage** There are no libraries explicitly mentioned in the provided JSON. However, some modern browsers use optimized implementations of these methods. **Special JS Features or Syntax** None are used directly in the benchmarking code. The focus is on comparing the performance of different algorithms. **Benchmark Results** The latest benchmark results show that: * **Filter**: Chrome 84 performed best with an execution speed of approximately 281 executions per second. * **Map**: Chrome 84 had a slower execution speed, around 71 executions per second. * **Obj (Object property removal)**: Chrome 84 had the slowest execution speed, around 57 executions per second. * **foundIndex**: Chrome 84 was the slowest overall, with an execution speed of about 0.41 seconds. **Other Alternatives** While not included in this benchmark, other approaches for finding and removing elements from arrays might include: * Using `splice()` instead of `indexOf()` * Using a binary search algorithm * Using a custom implementation that takes advantage of hardware features (e.g., SIMD instructions) Keep in mind that the choice of approach depends on the specific use case, performance requirements, and compatibility considerations.
Related benchmarks:
Fill array with random integers
Flatten Array of Arrays
Right shift VS Divide and round
.at vs length -1
Array.from VS spreading for
Comments
Confirm delete:
Do you really want to delete benchmark?