Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
path functions + reduce
(version: 0)
Comparing performance of:
new f vs reduce vs reduce recalculate
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var path = (keys, is = "return " + keys.reduce((values, key) => values + "['"+ key +"']", "value")) => new Function("value", is) var path2 = keys => value => keys.reduce((value, key) => value[key], value); var prop = key => value => value[key]; var identity = value => value; var path3 = keys => keys.reduce((call, key) => value => prop(key)(call(value)), identity); var keys = Array.from({ length: 100 }, (_, i) => "a" + i); var i = 65535; var test = {}; var tmp; while(i--) { tmp = test; test = { ["a" + i]: tmp }; } var run1 = path(keys); var run2 = path2(keys); var run3 = path3(keys); i = 1000000; while (i--) { identity(i); prop(i)([]); run1(Object.setPrototypeOf({ i }, test)); run2(Object.setPrototypeOf({ i }, test)); run3(Object.setPrototypeOf({ i }, test)); }
Tests:
new f
run1(test);
reduce
run2(test);
reduce recalculate
run3(test);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
new f
reduce
reduce recalculate
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 provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of three different approaches to creating a path function, which is used to dynamically create property names in an object. The goal is to determine which approach is the most efficient. **Approaches Compared** There are three approaches compared: 1. **`path`**: This function creates a new function that takes an initial value and uses the `reduce` method to concatenate key strings with quotes. It's then used to dynamically create properties in an object. 2. **`path2`**: This approach uses the `reduce` method directly on an array of keys to generate property names, without creating a new function. 3. **`path3`**: This approach creates a nested function by using `reduce` to concatenate key strings with quotes and then applying a `prop` function (explained later) to create properties. **Pros and Cons of Each Approach** 1. **`path`**: * Pros: Creates a self-contained function that can be reused, which might improve performance. * Cons: Requires creating an additional function, which could incur overhead. 2. **`path2`**: * Pros: Uses built-in `reduce` method, which is likely to be optimized by the browser. * Cons: Creates a new array and performs multiple iterations of `reduce`, which might be slower than a single execution of `reduce`. 3. **`path3`**: * Pros: Avoids creating an additional function, which could reduce overhead. * Cons: Uses nested functions, which can lead to increased complexity and potential performance issues. **Library Used** None, as all approaches use native JavaScript methods and functions. **Special JS Features/Syntax** The benchmark uses a few special features: 1. **`Object.setPrototypeOf`**: Used to set the prototype of an object, which affects property lookup. 2. **`reduce` method**: A built-in array method used to iterate over arrays and concatenate strings. 3. **`prop` function**: A custom function that takes a key and returns a new function that applies that key to its argument. **Other Considerations** The benchmark also considers: 1. **Browser and platform variations**: The benchmark runs on Chrome 114 on Linux, which might affect performance due to differences in implementation. 2. **Execution frequency**: The benchmark executes the functions 1 million times each, which can impact performance due to warm-up effects. **Alternatives** If you want to explore alternative approaches or libraries for path creation, consider: 1. **`lodash.path`**: A popular library that provides a `path` function for dynamic property creation. 2. **`underscore.path`**: Another well-known library that offers a similar path creation function. Keep in mind that benchmarking and exploring different approaches can be complex, and results may vary depending on the specific use case and implementation details.
Related benchmarks:
Object.fromEntries vs reduce vs property assignment
Object.keys vs Object.values vs Object.entries creation
for-in vs object.keys vs object.values for objects perf 5
map-values
reducer vs fromEntries
Comments
Confirm delete:
Do you really want to delete benchmark?