Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TestsPerf2
(version: 0)
Comparing performance of:
@john36allTa vs @dimoff66 vs @Bavashi
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var len = 3000; function init() { for (var i = 0; i < len; i++) { arr.push({ id: i+1, name: i+" ...", parent: i }); }} init();
Tests:
@john36allTa
function pack( data ){ const childs = id => data.filter( item => item.parent === id ) .map( ({id,name}) => ({id,name, children: childs(id)}) ).map( ({id,name,children}) => children.length ? {id,name, children} : { id, name } ); return childs(0); } const res = pack(arr)
@dimoff66
function pack( arr ) { const map = Object.assign({} , ...arr.map(v => ({ [v.id]: Object.assign(v, { children: [] }) }) )) const tree = Object.values(map).filter(v => !(v.parent && map[v.parent].children.push(v)) ) return tree } const res = pack(arr)
@Bavashi
function makeTree(array, parent) { var tree = {}; parent = typeof parent !== 'undefined' ? parent : {id: 0}; var childrenArr = array.filter(function(child) { return child.parent == parent.id; }); if (childrenArr.length > 0) { var childrenObj = {}; childrenArr.forEach(function(child) { childrenObj[child.id] = child; }); if (parent.id == 0) { tree = childrenObj; } else { parent.children = childrenObj; } childrenArr.forEach(function(child) { makeTree(array, child); }) } return tree; }; const res = makeTree(arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
@john36allTa
@dimoff66
@Bavashi
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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark on MeasurThat.net, where users can create and run benchmarks to compare the performance of different approaches. **Script Preparation Code** The script preparation code initializes an array `arr` with 3000 elements, each containing `id`, `name`, and `parent` properties. The purpose of this code is to provide a fixed dataset for the benchmarking tests. **Html Preparation Code** There is no HTML preparation code provided in this benchmark. **Individual Test Cases** The benchmark consists of three test cases: 1. **@john36allTa** This test case uses the `pack` function, which takes an array as input and returns a tree data structure. The implementation uses the `filter`, `map`, and `reduce` methods to construct the tree. 2. **@dimoff66** This test case also uses the `pack` function, but with a slight modification. Instead of using `Object.assign` to merge objects, it directly maps over the array elements and assigns the resulting objects to an object map. The implementation then filters out nodes without children and constructs the tree. 3. **@Bavashi** This test case uses a custom function called `makeTree`, which takes an array and a parent node as input. The implementation iterates over the array, filtering nodes with the same parent ID, and recursively constructs the tree. **Library Usage** None of the test cases rely on any external libraries or frameworks. **Special JavaScript Features/Syntax** The `pack` function in both @john36allTa and @dimoff66 uses an arrow function syntax, which is a concise way to define small, anonymous functions. This syntax was introduced in ECMAScript 2015 (ES6) and has since become a standard feature in modern JavaScript. **Pros and Cons of Different Approaches** Here's a brief analysis of the pros and cons of each approach: 1. **@john36allTa** * Pros: Easy to read, uses built-in methods for filtering and mapping. * Cons: May be less efficient due to the use of `Object.assign` in the original implementation (not used here). 2. **@dimoff66** * Pros: Avoids using `Object.assign`, which can be slower than direct property assignment. * Cons: Uses an outdated syntax for creating objects and may be harder to read for those unfamiliar with ES6 features. 3. **@Bavashi** * Pros: Custom implementation allows for fine-grained control over the tree construction process. * Cons: More verbose than the other two approaches, which may make it less readable. **Other Alternatives** If you were to rewrite these test cases using a different approach, you could consider the following alternatives: 1. Use a library like Lodash or Ramda to provide more concise and expressive implementations for filtering and mapping operations. 2. Implement the `makeTree` function in a way that avoids recursive calls, such as using an iterative approach with stacks or queues. 3. Use a different data structure, such as a linked list or a graph, to represent the tree instead of objects. Ultimately, the choice of implementation depends on your personal preference, experience, and performance requirements.
Related benchmarks:
Array clone
Test native unique
Speed of Arr.length and Slice
Testing Spread 21062023
+ vs Number, with 100k numbers
Comments
Confirm delete:
Do you really want to delete benchmark?