Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
GetUniqueSortedArray
(version: 0)
Comparing performance of:
reduce vs foreach
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = { a: { e: 25, x: 3, y: { d: 2, e: 2, f: { g: 4 }, z: 8 }, f: { f: { d: { b: { a: 15, b: 34 } } } } } };
Tests:
reduce
const getSortedReduce2 = (obj) => { return Object.entries(obj).reduce((acc, [key, value]) => { if (isNaN(value)) { acc = acc.concat(getSortedReduce2(obj[key])); } else { if (!acc.includes(value)) { acc = acc.concat(value); } } return acc.sort((a, b) => a - b); }, []); }; getSortedReduce2(a);
foreach
const e = []; const getSortedNumber = (obj) => { Object.entries(obj).forEach(([key, value]) => { if (!isNaN(value)) { if (!e.includes(value)) { e.push(value); } } else { getSortedNumber(obj[key]); } }); }; getSortedNumber(a); e.sort((a, b) => a - b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
foreach
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 what is being tested in the provided JSON benchmark. **Benchmark Definition** The first part of the JSON defines a benchmark named "GetUniqueSortedArray". This benchmark consists of a JavaScript object `a` with nested properties, including some numbers and other objects. The benchmark does not specify any particular task to be performed, but rather provides a setup for two test cases. **Test Cases** There are two test cases: 1. **reduce**: This test case uses the `reduce()` method on an array of entries from the object `a`. The function passed to `reduce()` checks if each value is not a number (using `isNaN()`) and, if so, recursively calls itself on the corresponding nested object. If the value is a number, it adds it to an array `e` if it's not already present. 2. **foreach**: This test case uses a similar approach as the previous one but without using `reduce()`. Instead, it iterates over the entries of the object using `forEach()` and performs the same checks and recursive calls. **Options Compared** The two test cases compare two different approaches to achieve the same goal: * **Reduce**: Uses the built-in `reduce()` method on an array. * **Foreach**: Manually iterates over the entries using a loop (`forEach()`). **Pros and Cons** Here are some pros and cons of each approach: * **Reduce**: * Pros: Concise, efficient, and can be used with many other functions (e.g., `map()`, `filter()`). * Cons: Can be less readable for complex logic or when working with non-array data structures. * **Foreach**: * Pros: More explicit, easier to understand for complex logic or when working with non-array data structures. However, it can be slower due to the overhead of the `forEach()` call. In general, choose `reduce()` when you need a concise and efficient solution that works well with arrays. Use `foreach` when you want more control over the iteration process or when working with non-array data structures. **Library/Function Used** Both test cases use built-in JavaScript functions: `Object.entries()` for accessing entries of an object, `forEach()` for iterating over an array, and `reduce()` for applying a function to each element of an array. No external libraries are required for these tests. **Special JS Features/Syntax** There is no special JavaScript feature or syntax used in this benchmark. Both test cases only utilize built-in functions and standard object properties (e.g., `Object.entries()`, `forEach()`). **Alternatives** If you wanted to compare the performance of different approaches, here are some alternatives: * **Map vs Reduce**: You could replace one of the test cases with a version that uses `map()` instead of `reduce()`. This would demonstrate how map and reduce differ in terms of their approach. * **Other Iteration Methods**: In addition to `forEach()` and `reduce()`, you might consider using other iteration methods like `for...in` or `Array.prototype.forEach.call()` (the latter being the same as `forEach()` but explicitly invoking it on an array). * **Parallelization**: You could modify one of the test cases to perform some operation in parallel with others, demonstrating how performance scales when more operations are performed simultaneously. These alternatives would provide a broader picture of JavaScript iteration methods and their performance characteristics.
Related benchmarks:
array push
Array.from vs Array spread with mapping of values
Test of unshift methods
spread vs ArrayFrom
11111111
Comments
Confirm delete:
Do you really want to delete benchmark?