Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Pull from JS
(version: 0)
Comparing performance of:
Array vs Object vs Map
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const myArr =[]; const myMap = new Map(); const myObj = {}; function getFromArr(key){ return myArr.find(n=>n.id===key); } function getFromMap(key){ return myMap.get(key); } function getFromObj(key){ return myObj[key]; } // PUPULATE DATA const size = 25000; let val, key; for (let i=0;i<size;i++){ key = `file_${i}`; val = Math.floor ((Math.random()*100000)); myArr.push({ id: `file_${i}`, value: val }); myMap.set(key,val); myObj[key] = val; } const TEST_ITERATIONS = size; function runPerfOnFunction(funcToPull, iterationCount, testName){ for (let i=0;i<iterationCount;i++){ let id = Math.floor ((Math.random()*size)) key = `file_${id}`; funcToPull(key); } }
Tests:
Array
runPerfOnFunction(getFromArr, 25000, 'myArr Pull');
Object
runPerfOnFunction(getFromObj, 25000, 'myObj Pull');
Map
runPerfOnFunction(getFromMap, 25000, 'myMap Pull');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array
Object
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 the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark definition for three different data structures: an array, an object, and a map. The script preparation code defines three functions: * `getFromArr(key)`: Retrieves an element from the array by its index (`id`). * `getFromMap(key)`: Retrieves a value from the map by its key. * `getFromObj(key)`: Retrieves a property from the object by its key. The script also populates these data structures with 25,000 elements and defines a function `runPerfOnFunction(funcToPull, iterationCount, testName)` that runs each of these functions for a specified number of iterations. **Options Compared** In this benchmark, two options are compared: 1. **Array**: The `getFromArr` function is used to retrieve elements from the array. 2. **Object**: The `getFromObj` function is used to retrieve properties from the object. 3. **Map**: The `getFromMap` function is used to retrieve values from the map. **Pros and Cons of Each Approach** Here's a brief analysis of each approach: * **Array**: + Pros: Fast lookup times, as arrays are implemented as hash tables. + Cons: Searching for an element by its index can be slower than searching by a key (like in maps or objects). * **Object**: + Pros: Fast lookup times when using the `in` operator or bracket notation (`[key]`), which is optimized by modern JavaScript engines. + Cons: Slower than arrays for searching by index, and can be slower for large numbers of properties. * **Map**: + Pros: Fast lookup times when using the `.get()` method, which is optimized by modern JavaScript engines. + Cons: Requires the key to exist in the map; if it doesn't, `get()` will return `undefined`. **Library and Purpose** None of the libraries used in this benchmark are explicitly mentioned in the provided JSON. However, we can infer that the `Map` object is a built-in JavaScript feature since its implementation has improved over time. **Special JS Features or Syntax** There's no explicit use of special JavaScript features or syntax in this benchmark. The code uses basic functions and data structures. **Alternatives** If you want to optimize your own data structures or search algorithms, consider the following alternatives: 1. **Bloom filters**: For fast membership testing (i.e., checking if an element exists) in arrays or sets. 2. **Hash tables**: For faster lookup times than linear searches in arrays or objects. 3. **S suffix trees**: For efficient searching of substrings in strings. Keep in mind that each alternative has its own trade-offs and requirements for implementation and usage. In conclusion, the MeasureThat.net benchmark provides a simple yet informative comparison of three data structures: array, object, and map. By understanding the pros and cons of each approach, developers can make more informed decisions about choosing the right data structure or search algorithm for their specific use case.
Related benchmarks:
For loop map vs map builtin for 10000000 elements
For loop map vs map builtin for 100000 elements
Iterate Map entries
Map loop vs foreach
Large Map vs Object 2
Comments
Confirm delete:
Do you really want to delete benchmark?