Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Building objects
(version: 2)
Benchmarking building a collection of models from arrays of keys & values
Comparing performance of:
Iterate keys to build objects vs Build new fn
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.keys = []; window.values = []; const NUM_KEYS = 100; const COLLECTION_SIZE = 500; const s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; function randString(len) { return Array(len).fill('').map(() => s.charAt(Math.floor(Math.random() * s.length))).join(''); } // Build 100 keys for (let i = 0; i < NUM_KEYS; i++) { keys[0] = randString(10); } // Fill array-of-arrays with values for (let i = 0; i < COLLECTION_SIZE; i++) { values[i] = []; for (let j = 0; j < NUM_KEYS; j++) { values[i][j] = randString(10); } }
Tests:
Iterate keys to build objects
const collection = Array(values.length); for (let i = 0; i < values.length; i++) { const obj = collection[i] = {}; const theseVals = values[i]; for (let j = 0; j < keys.length; j++) { obj[keys[j]] = theseVals[j]; } }
Build new fn
function makeFlipFn(keys) { return new Function('v', 'idx', ` return { ${keys.map((key, i) => `"${key}": v[${i}][idx]`).join(',')} } `); } const buildFn = makeFlipFn(keys); const collection = Array(values.length); for (let i = 0; i < values.length; i++) { collection[i] = buildFn(values[i], i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Iterate keys to build objects
Build new fn
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):
I'll explain the benchmark and its options. The provided JSON represents a JavaScript microbenchmark created using MeasureThat.net. The benchmark is designed to test two different approaches for building an object from an array of keys and values. **Benchmark Definition** The "Building objects" benchmark definition explains that it measures how fast an array can be populated with objects, where each object has properties corresponding to the indices in the original array. **Options Compared** There are two options compared: 1. **Iterate Keys**: This approach uses a traditional loop to iterate over the keys and build the objects. 2. **Build new fn**: This approach creates a new function that takes an array of values as input and returns an object with properties corresponding to the indices in the original array. **Pros and Cons** * **Iterate Keys** + Pros: Simple and straightforward, easy to understand and maintain. + Cons: May be slower due to the overhead of repeated function calls or updates to the object. * **Build new fn** + Pros: Can potentially be faster since it avoids repeated function calls or object updates. + Cons: More complex and harder to understand, as it involves creating a new function. **Library and Special JS Feature** There is no library used in this benchmark. However, the `makeFlipFn` function created in the "Build new fn" approach uses a feature called **Function Expression**, which allows defining functions inline within larger expressions. **Other Considerations** * The use of `Array.fill` to initialize the `keys` and `values` arrays might affect performance. * The `randString` function used to generate random strings may introduce variability in the benchmark results. * The device platform, operating system, and browser being tested (Chrome 64 on a Mac OS X 10.12.6) might influence the results due to differences in hardware or software configuration. **Alternatives** Other approaches for building objects from arrays of keys and values could include: 1. Using `Object.assign()` to create an object with properties corresponding to the indices. 2. Utilizing libraries like Lodash or Ramda, which provide functions for working with objects and arrays. 3. Leveraging modern JavaScript features like template literals or computed property names. Note that the choice of approach often depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
for i < length vs .forEach(t) vs for..of vs for t = keys[i] vs for i =0; i in keys vs for i in object vs .reduce (keys only)
Object.keys().length vs for i in object i++ vs Object.entries().length vs Object.values().length
Object vs Map lookup w/ rando integer key and array
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?