Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Array Filtering
(version: 0)
Comparing performance of:
set vs array
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.TYPES_ARRAY = ["extension", "spawn"] window.TYPES_SET = {"extension": true, "spawn": true} window.OBSTACLE_OBJECT_TYPES = ["spawn", "creep", "powerCreep", "source", "mineral", "deposit", "controller", "constructedWall", "extension", "link", "storage", "tower", "observer", "powerSpawn", "powerBank", "lab", "terminal", "nuker", "factory", "invaderCore"] window.objects = [] for (let i = 0; i < 100; i++) { objects.push(Math.floor(Math.random() * OBSTACLE_OBJECT_TYPES.length)) }
Tests:
set
console.log(objects.filter(v => TYPES_SET[v]).length)
array
console.log(objects.filter(v => TYPES_ARRAY.includes(v)).length)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
array
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 definition and test cases. **Benchmark Definition:** The benchmark definition is a JSON object that provides metadata about the benchmark, such as its name, description, and preparation code. In this case, we have two benchmark definitions: 1. "Set vs Array Filtering" 2. "console.log(objects.filter(v => TYPES_SET[v]).length)" The first one is a generic name for the benchmark, while the second one is the actual JavaScript code that will be executed during the benchmark. **Script Preparation Code:** The script preparation code sets up variables and objects used in the benchmark. Specifically: * `window.TYPES_ARRAY` and `window.TYPES_SET` define two types of values that can be used in the filter function. * `window.OBSTACLE_OBJECT_TYPES` is an array of object types used to generate random objects. * The script then creates an array of 100 random objects using these types. **Html Preparation Code:** There is no HTML preparation code provided, which means that no additional setup or rendering is required before running the benchmark. **Individual Test Cases:** We have two individual test cases: 1. "set" * `console.log(objects.filter(v => TYPES_SET[v]).length)` 2. "array" * `console.log(objects.filter(v => TYPES_ARRAY.includes(v)).length)` These test cases differ in how they use the `filter` function to process the objects array. **Library:** There is no explicit library mentioned in the benchmark definition, but we can infer that the `Array.prototype.includes()` method is used in the second test case. This method is a part of the built-in JavaScript Array prototype and does not require any external libraries. **Special JS Feature/Syntax:** The `filter` function and template literals (`v => TYPES_SET[v]`) are standard JavaScript features, but the use of `includes()` in the second test case might be less common. However, it's a valid syntax that has been part of JavaScript since ECMAScript 2015 (ES6). **Pros and Cons:** 1. **Array Filtering (using includes())** * Pros: + Fast and efficient + Easy to implement * Cons: + May not be as expressive or flexible as other filtering approaches 2. **Set Filtering (using in operator)** * Pros: + More expressive and flexible than using includes() + Can handle more complex filtering scenarios * Cons: + May be slower than using includes() due to the overhead of creating a set **Other Alternatives:** 1. **Array.prototype.some():** Instead of using `includes()` or the `filter()` method, you can use the `some()` method to achieve similar results. 2. **Lodash.filter():** If you need more complex filtering scenarios, you can use a library like Lodash, which provides a `filter()` function that can handle more advanced filtering logic. In summary, the benchmark definition tests two different approaches to filtering an array of objects: using the `includes()` method and using the `in` operator with a set. The pros and cons of each approach are discussed above, and alternative methods are also presented for more complex filtering scenarios.
Related benchmarks:
somevsfind
A.push(B) V.S. [ ...A, B ]
spread vs for loop for array of objects into one object
set has vs array includes (1k items)
set vs array add 1k items
Comments
Confirm delete:
Do you really want to delete benchmark?