Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
loading transformed {}[] values into Map: for of Map.set vs for of new Map ( results{}[] ) vs new Map ( {}[].map )
(version: 0)
Comparing performance of:
for of Map set vs for of new Map( results[] ) vs new Map( {}[].map )
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var test = /\s*(?<key>[^=\s=;][^=;]*?)(?:\s*=\s*(?<value>[^;]+?))?(?:;|$)/g var sample = ' test1=1; test2; test 3; test 4= 4; test5 =5; test 6 = 6; test 7 = 777=77='
Tests:
for of Map set
const map = new Map for ( const { key, value } of sample.matchAll( test ) ) { map.set( key, value ) }
for of new Map( results[] )
const results = [] for ( const { key, value } of sample.matchAll( test ) ) { results.push( [ key, value ] ) } const map = new Map( results )
new Map( {}[].map )
const map = new Map ( [...sample.matchAll( test )] .map( ( { key, value } ) => [ key, value ] ) )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for of Map set
for of new Map( results[] )
new Map( {}[].map )
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):
Measuring performance is crucial in software development, and using benchmarks like MeasureThat.net helps identify bottlenecks and optimize code. The provided benchmark tests three approaches to loading transformed JSON values into a Map: 1. **for...of** with `Map.set` (Test Name: "for of Map set") 2. **for...of** with creating an array and then passing it to `new Map` (Test Name: "for of new Map( results[] )") 3. **Array.prototype.map()` followed by `new Map` creation (Test Name: "new Map( {}[].map )") Let's break down each approach: ### for...of with Map.set This method uses the `for...of` loop to iterate over the transformed JSON values and sets each key-value pair directly on the Map. **Pros:** * Efficient, as it avoids creating unnecessary intermediate data structures. * Directly accesses the desired property using the `key` variable, reducing memory allocation and copying overhead. **Cons:** * May incur additional overhead due to the `for...of` loop's dynamic nature, which can lead to slower execution compared to other approaches. ### for...of with new Map(results[]) This approach uses the `for...of` loop to iterate over the transformed JSON values, creates an array `results`, and then passes this array to the `new Map()` constructor. The resulting Map is then assigned to a variable. **Pros:** * Allows for easy debugging and visualization of the data by inspecting the created Map. * Can be useful when working with large datasets or complex data structures. **Cons:** * Creates an additional intermediate array, which can lead to increased memory allocation and copying overhead. * May incur slower execution due to the creation of this extra data structure. ### new Map({}.map()) This approach uses the `Array.prototype.map()` method to transform the JSON values into an array, and then creates a new Map from this array using the `new Map()` constructor. **Pros:** * Eliminates the need for an intermediate array, reducing memory allocation and copying overhead. * Can be beneficial when working with large datasets or complex data structures. **Cons:** * May incur additional overhead due to the creation of the temporary array, which can lead to slower execution compared to other approaches. The provided benchmark results show that: * The "for of Map set" approach is the fastest (Execution Per Second: 175572.25). * The "new Map( {}[].map )" approach is the second-fastest (Execution Per Second: 154948.09375). * The "for of new Map( results[] )" approach is the slowest (Execution Per Second: 167621.5625). Other considerations: * **Library usage**: None of the benchmark cases explicitly uses any libraries or external dependencies. * **Special JS features/syntax**: The `const` keyword and template literals are used in some parts of the code, but their use is not a focus of this particular benchmark. Alternatives to these approaches include: * Using other iteration methods, such as `forEach()` or `reduce()`, to load JSON values into a Map. * Utilizing built-in JavaScript functions like `Array.from()` and `Map.of()` for creating Maps from arrays. * Exploring other data structures, such as Sets or Objects, depending on the specific requirements of your use case. Keep in mind that performance optimizations should be based on the specific needs and constraints of your project.
Related benchmarks:
for vs map
Array range generating
Map has vs get
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
Test push spread map2
Comments
Confirm delete:
Do you really want to delete benchmark?