Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
us object vs reduce
(version: 0)
Comparing performance of:
under vs over
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'></script>
Script Preparation code:
var contextDefinitions = [{ name: "meep", dataType: 1 }, { name: "moop", dataType: 1 }, { name: "egberg", dataType: 1 }, { name: "QWER", dataType: 1 }, { name: "nbtrbh", dataType: 1 }, { name: "wefwef", dataType: 1 }, { name: "ergwerg", dataType: 1 }, { name: "ethgerg", dataType: 1 }]; var values = ["etg", "erg", "werg", "erth", "rtyj", "aqery", "aerb", "srbntr"];
Tests:
under
_.object( contextDefinitions.map((contextPair, index) => { return [ contextPair.name.toLowerCase(), { value: values[index], dataType: contextPair.dataType } ]; }) )
over
contextDefinitions.reduce((acc, contextPair, index) => acc[contextPair.name.toLowerCase()] = {value: values[index], dataType: contextPair.dataType}, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
under
over
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 break down the provided benchmark definition and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare two ways of creating an object in JavaScript: 1. **_.object()` (Under)**: This method takes an array of objects as input, where each object has a `name` property and a value with a specific `dataType`. The resulting object will have the same properties with corresponding values. 2. **`.reduce()`** (Over): This method reduces an array to a single object by iterating over it and adding properties to the accumulator. **Options Compared** The two options are compared in terms of performance, which is measured by the number of executions per second. **Pros and Cons** * **_.object() (Under)**: + Pros: More explicit and straightforward way of creating an object. + Cons: Might be slower due to the overhead of creating a new object and iterating over the array. * **`.reduce()`** (Over): + Pros: Can be faster since it's optimized for creating objects by reducing arrays. + Cons: Less readable and explicit, as it uses a callback function to build the resulting object. **Library** The `underscore.js` library is used in this benchmark. `_` is an alias for the Underscore.js library, which provides functional programming utilities, including the `_.object()` method. **Special JS Feature or Syntax** There doesn't appear to be any special JavaScript features or syntax being used in this benchmark. **Other Considerations** * The benchmark uses a small dataset (8 objects) to keep it simple and focused on object creation performance. * The script preparation code defines the `contextDefinitions` array, which contains objects with `name` and `dataType` properties. This will be used as input for both _.object() and .reduce(). * The HTML preparation code includes a reference to Underscore.js, which is loaded via a CDN. **Alternatives** Other alternatives for creating objects in JavaScript might include: 1. **Object.create()**: A more traditional way of creating an object by passing a prototype. 2. **Array.prototype.map() + Object.assign()**: Another way to create an object from an array using `map()` and `Object.assign()`. 3. **For...of loop with Object**: Creating an object directly in the loop, without relying on libraries. These alternatives might be worth considering in a different benchmarking scenario, but for this specific benchmark, _.object() and `.reduce()` are focused on performance.
Related benchmarks:
Lodash groupBy vs JS reduce
Lodash map & filter vs reduce with push and desctructuring (10 000 samples )
Lodash reduce vs transform vs Native reduce on object
Lodash reduce vs transform vs Native reduce on object (Object without prototype)
lodash groupBy vs Array.reduce with map
Comments
Confirm delete:
Do you really want to delete benchmark?