Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
assoc vs map
(version: 0)
testing adding keys to map vs assoc props to obj
Comparing performance of:
obj vs map
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/rxjs@^7/dist/bundles/rxjs.umd.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Script Preparation code:
let obj = {}; let map = new Map();
Tests:
obj
let obs = rxjs.of(...R.range(1, 1000)); obs.subscribe(val => ramda.assoc(`key-${val}`, val, arr));
map
let obs = rxjs.of(...R.range(1, 1000)); obs.subscribe(val => map.add(`key-${val}`, val));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
obj
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):
Let's dive into explaining what is tested on the provided JSON and other considerations. The benchmark compares two approaches: **Approach 1: Associating properties with an object (obj)** This approach uses the `ramda` library, specifically the `assoc` function, which associates a value with an existing property of an object. The test code creates an empty object `obj`, generates an array of values from 1 to 1000 using `rxjs.of` and `R.range`, and then subscribes to the observable, associating each value with the corresponding key in the object. ```javascript let obs = rxjs.of(...R.range(1, 1000)); obs.subscribe(val => ramda.assoc(`key-${val}`, val, arr)); ``` **Approach 2: Adding properties to a Map (map)** This approach uses the `rxjs` library, specifically the `of` function, and the `Map` data structure from the browser's built-in API. The test code creates an empty Map `map`, generates an array of values from 1 to 1000 using `rxjs.of` and `R.range`, and then subscribes to the observable, adding each value as a key-value pair to the Map. ```javascript let obs = rxjs.of(...R.range(1, 1000)); obs.subscribe(val => map.add(`key-${val}`, val)); ``` **Pros and Cons:** * **Approach 1 (obj)**: + Pros: Can be more efficient in terms of memory allocation and garbage collection, as objects are mutable and can reuse existing properties. + Cons: Can lead to slower performance if the object is large, as each property association requires a new allocation. * **Approach 2 (map)**: + Pros: Efficient in terms of memory allocation and garbage collection, as Maps are designed for fast lookup and insertion. + Cons: May require more computational resources, especially when dealing with large datasets. **Other Considerations:** * The benchmark uses two different libraries (`ramda` and `rxjs`) to test the approaches. This allows users to compare performance between different libraries and implementations. * The tests use a fixed range of values (1-1000) and generate an array of these values using `rxjs.of` and `R.range`. This ensures that the tests are consistent and controlled. * The benchmark reports execution counts per second for each test case, providing users with an estimate of performance. **Library Descriptions:** * **Ramda**: A functional programming library that provides a set of higher-order functions for manipulating arrays, objects, and other data structures. In this benchmark, `assoc` is used to associate values with existing properties of objects. * **RxJS**: A reactive extensions library that provides observables and operators for handling asynchronous data streams. In this benchmark, `of` is used to generate an array of values from 1 to 1000. If there are any special JavaScript features or syntaxes mentioned in the explanation, please let me know, and I'll be happy to elaborate!
Related benchmarks:
ForOf+Push vs Map
Object spread vs New map
Object spread vs New map with string keys
Object spread vs New map entries
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?