Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Recreate Object vs Map vs Array vs Set
(version: 1)
Comparing performance of:
Create Object vs Create Map vs Create Array vs Create Set
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = {"one": true, "two": true, "three": true, "four": true, "five": true, "six": true, "seven": true, "eight": true, "nine": true, "ten": true}; var map = new Map([["one", true], ["two", true], ["three", true], ["four", true], ["five", true], ["six", true], ["seven", true], ["eight", true], ["nine", true], ["ten", true]]); var arr = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]; var set = new Set(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]);
Tests:
Create Object
var obj1 = {...obj};
Create Map
var map1 = new Map(map);
Create Array
var arr1 = [...arr];
Create Set
var set1 = new Set(set);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Create Object
Create Map
Create Array
Create Set
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 and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of creating objects, maps, arrays, and sets in JavaScript. The test case uses a predefined JSON object (`obj`) as input to create each type of data structure. **Options Compared** The benchmark tests four options: 1. **Object**: Creating an object using the literal syntax (`var obj = {...obj};`). 2. **Map**: Creating a Map using the `new Map()` constructor and iterating over the input array (`var map = new Map([[\"one\", true], [\"two\", true], ...])`). 3. **Array**: Creating an array by spreading the input array (`var arr = [...arr];`). 4. **Set**: Creating a Set using the `new Set()` constructor and iterating over the input array (`var set = new Set([\"one\", \"two\", \"three\", ...])`). **Pros and Cons of Each Approach** 1. **Object**: This is the most straightforward way to create an object. However, it may lead to slower performance due to the overhead of creating a new object and assigning properties. 2. **Map**: Creating a Map can be faster than creating an object because Maps are optimized for lookups and insertions. However, this approach requires iterating over the input array, which can introduce additional overhead. 3. **Array**: Spreading the input array creates a shallow copy of the array, which can lead to slower performance due to the overhead of copying data. This approach is useful when you need to create an array with a specific structure. 4. **Set**: Creating a Set using `new Set()` and iterating over the input array has similar performance characteristics to creating a Map. **Library Used** None (builtin JavaScript APIs) **Special JS Features/Syntax** None mentioned in the benchmark definition, but note that some modern browsers support features like `const` and `let` declarations, which may affect performance. **Benchmark Result Interpretation** The benchmark result shows the number of executions per second for each test case. The top performer is creating an array (`var arr = [...arr];`), followed by creating a Set (`var set = new Set([\"one\", \"two\", \"three\", ...])`). Creating a Map comes in third, and creating an object has the lowest performance. **Other Alternatives** If you need to create objects, maps, arrays, or sets with specific properties or structures, consider using the following alternatives: * For objects: Use the `Object.create()` method or the `class` syntax. * For maps: Use the `Map.prototype.set()` method or the `class` syntax with a `Map` constructor. * For arrays: Use array methods like `push()`, `unshift()`, or `concat()`. * For sets: Use set methods like `add()`, `delete()`, or `has()`. Keep in mind that these alternatives may affect performance and should be used judiciously depending on your specific use case.
Related benchmarks:
flatMap() vs filter().map() - arrays
.flatMap vs .map.flat
create new Map vs create new Array
flatMap + flatMap vs flat(2)+map
flat() vs flatMap()
Comments
Confirm delete:
Do you really want to delete benchmark?