Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set delete vs array splice 2
(version: 0)
Comparing performance of:
set delete vs arr
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = [...Array(1000).keys()] var set = new Set(arr)
Tests:
set delete
set.add(1001)
arr
arr.includes(1001) arr.push(1001)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set delete
arr
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set delete
49976056.0 Ops/sec
arr
942673.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the JavaScript microbenchmark on MeasureThat.net. **Benchmark Definition** The benchmark defines two test cases: "set delete" and "arr". The first test case measures the performance of adding an element to a `Set` object, while the second test case measures the performance of checking if an element exists in an array and then pushing that same element onto the array. The `Script Preparation Code` sets up both test cases by creating a large array (`arr`) and a new `Set` object (`set`) containing all elements from the array. **Options Compared** The benchmark compares two approaches for adding an element to the set: 1. **set.add(1001)**: This approach uses the `add()` method of the `Set` object, which is likely implemented in native code and optimized for performance. 2. **arr.includes(1001) + arr.push(1001)**: This approach uses the `includes()` method to check if an element exists in the array, and then pushes that same element onto the array using the `push()` method. **Pros and Cons** **set.add(1001)** Pros: * Likely implemented in native code, making it optimized for performance. * Typically faster than equivalent JavaScript operations. Cons: None mentioned, as this approach is expected to be the fastest. **arr.includes(1001) + arr.push(1001)** Pros: * This approach uses built-in methods that are widely supported and well-maintained in modern browsers. * It may be more readable and easier to understand for developers familiar with array manipulation. Cons: * May not be as optimized or fast as native code implementations, potentially due to overhead from JavaScript interpreter. * The `push()` method is called twice, which could result in additional overhead compared to simply using the `add()` method. **Library: Set** The `Set` object is a built-in JavaScript data structure introduced in ECMAScript 2015 (ES6). It provides an efficient way to store and manipulate unique values. The `add()` method is optimized for performance and is implemented in native code, making it suitable for microbenchmarks like this one. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark. However, the use of ES6 syntax (e.g., `var`, arrow functions, etc.) is expected to be compatible with most modern browsers. **Other Alternatives** If you're interested in exploring alternative approaches for adding elements to a set, here are a few options: * Using an array and checking if the element already exists using `indexOf()`: `arr.indexOf(1001) === -1 && arr.push(1001)` * Using the `filter()` method: `arr.filter(x => x !== 1001).push(1001)` * Implementing a custom `add()` method for a different data structure (e.g., a linked list, tree, etc.) Keep in mind that these alternatives may not be as optimized or efficient as the native `Set` implementation.
Related benchmarks:
Split vs slice vs Splice vs Set javascript
splice vs set javascript
splice vs set javascript
set delete vs array splice
Comments
Confirm delete:
Do you really want to delete benchmark?