Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map clearance
(version: 0)
Comparing Array.from(Map.values()) vs [].push(Map.values().next())
Comparing performance of:
Use array methods vs Use map enumerator
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var i = 0, count = 100000, a; var map = new Map(); for (i = 0; i < count; i++) { if (Math.round(Math.random() * 1000)) { map.set(i, i * i); } }
Tests:
Use array methods
var values = Array.from(map.values()); var chunkSize = 2000; var count = 1; var chunk = []; for (let i = 0; i < values.length; i += chunkSize) { chunk = values.slice(i, i + chunkSize); console.log(count, chunk.length); count++; }
Use map enumerator
var chunk = []; var chunkSize = 2000; var count = 1; for (let [a,b] of map) { if(chunk.length <= chunkSize) { console.log(count, chunk.length); chunk = []; count++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Use array methods
Use map enumerator
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 its components. **Benchmark Definition** The benchmark is designed to compare two approaches for clearing a Map object: 1. Using `Array.from(Map.values())` to get an array of values from the Map, and then pushing these values onto an empty array using `[] .push()`. 2. Using the Map's iterator (`for...of`) loop to iterate over its key-value pairs, collecting them into an array. **Options Compared** The two options being compared are: 1. **Array.from(Map.values())**: This approach uses a built-in JavaScript method to create an array from the values of the Map. It is designed for general-purpose use cases where you need to work with arrays. 2. **Using map enumerator (`for...of`) loop**: This approach uses the iterator protocol to iterate over the key-value pairs of the Map, collecting them into an array. **Pros and Cons** 1. **Array.from(Map.values())**: * Pros: Efficient, concise, and easy to read. It creates a new array with the values from the Map. * Cons: Creates a new array, which can be memory-intensive for large datasets. 2. **Using map enumerator (`for...of`) loop**: * Pros: More control over iteration, no memory overhead since it uses a generator-like approach. * Cons: Less concise and more verbose than the first option. **Other Considerations** 1. Performance: The benchmark results show that the first option (using `Array.from(Map.values())`) is faster in this specific test case. 2. Memory usage: The second option (using the map enumerator loop) avoids creating a new array, which can be beneficial for large datasets or memory-constrained environments. **Library and Special JS Features** In this benchmark: 1. There are no external libraries mentioned. 2. No special JavaScript features or syntax are used beyond standard ECMAScript 2022 features (e.g., `for...of`, `let`/`const` declarations). **Alternatives** Other approaches for clearing a Map object might include: * Using the spread operator (`[...]`) to create a new array from the values of the Map. * Using a library like Lodash or Ramda for more complex operations on Maps. Keep in mind that these alternatives may have their own trade-offs in terms of performance, memory usage, and conciseness.
Related benchmarks:
for vs map
Map vs Array vs Object vs Set add item speed in 50000 iters 2
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?