Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Mapping (Key vs Index)
(version: 0)
Comparing performance of:
Lookup (Key based) vs Lookup (Index based)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let a = {}; for (let i = 0; i < 1000; i++) { a[`cond-${i}`] = []; for (let y = 0; y < 1000; y++) { a[`cond-${i}`].push(y); } } let b1 = []; let b2 = []; for (let i = 0; i < 1000; i++) { b1[i] = []; b2.push(`cond-${i}`); for (let y = 0; y < 1000; y++) { b1[i].push(y); } } function lookup_a(id) { return a[id]; } function lookup_b(id) { return b1[b2.indexOf(id)]; }
Tests:
Lookup (Key based)
for (let i = 0; i < 1000; i++) { lookup_a(`cond-${i}`) }
Lookup (Index based)
for (let i = 0; i < 1000; i++) { lookup_b(`cond-${i}`) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lookup (Key based)
Lookup (Index based)
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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches: looking up a value in an object using its key (also known as "key-based") versus looking up a value in an array using its index (also known as "index-based"). **Script Preparation Code** The script preparation code creates two objects, `a` and `b`, and populates them with arrays. The object `a` has 1000 properties, each containing an array of 1000 values. The array `b1` is initialized with 1000 empty arrays, while the array `b2` contains strings representing the property names in `a`. **Functions** Two functions are defined: * `lookup_a(id)`: takes a string as input and returns the corresponding value from object `a`. * `lookup_b(id)`: takes a string as input and returns the corresponding value from array `b1` using the index provided by the string in `b2`. **Test Cases** There are two test cases: * **Lookup (Key based)**: This test case calls `lookup_a(`cond-${i}`)` 1000 times, where `i` ranges from 0 to 999. * **Lookup (Index based)**: This test case calls `lookup_b(`cond-${i}`)` 1000 times, where `i` ranges from 0 to 999. **Options Compared** The benchmark is comparing the performance of two approaches: 1. **Key-based**: Looking up a value in an object using its key (using `lookup_a(id)`). 2. **Index-based**: Looking up a value in an array using its index (using `lookup_b(id)`). **Pros and Cons of Each Approach** * **Key-based**: + Pros: More readable and intuitive, easier to maintain. + Cons: Can be slower than index-based lookup due to the overhead of string manipulation and object lookups. * **Index-based**: + Pros: Can be faster than key-based lookup, especially for large arrays. + Cons: Less readable and more prone to errors, especially when dealing with complex array indices. **Library Used** In this benchmark, no specific library is used. The script only uses built-in JavaScript features and functions. **Special JS Feature or Syntax** This benchmark does not use any special JavaScript features or syntax beyond the standard features mentioned above. **Alternative Approaches** Other approaches to compare in this benchmark might include: * Using a hash table (e.g., `Map`) instead of an object for key-based lookup. * Using a binary search tree (BST) instead of an array for index-based lookup. * Using a different data structure, such as a linked list or a queue, for both key-based and index-based lookups. Note that the choice of approach depends on the specific use case and requirements.
Related benchmarks:
Map vs Object part 2
Map vs Object part 3
Map vs Object lookup part 5 - all string index
filterMapInPairs
Map vs Object read performance for a 1000 key lookup
Comments
Confirm delete:
Do you really want to delete benchmark?