Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
PerfTest
(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 = 1000; 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):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Definition** The provided JSON represents a benchmark definition, which outlines the test case for measuring the performance of different approaches to building a tree data structure from an array. Here's a breakdown of what's being tested: * The input is an array `arr` containing 1000 objects with `id`, `name`, and `parent` properties. * The goal is to create a tree data structure from this array, where each object represents a node in the tree, and its `parent` property indicates its parent-child relationship. **Options Compared** The benchmark compares three different approaches to building the tree: 1. **@john36allTa**: This approach uses an inline function `pack()` that takes the input array and returns the constructed tree. 2. **@dimoff66**: This approach defines a separate function `pack(arr)` that takes the input array and returns the constructed tree. 3. **@Bavashi**: This approach defines a recursive function `makeTree(array, parent)` that constructs the tree by traversing the array. **Pros and Cons of Each Approach** Here's a brief analysis of each approach: 1. **@john36allTa**: * Pros: Compact code, uses fewer lines than the other two approaches. * Cons: May be harder to read and understand due to its inline nature. 2. **@dimoff66**: * Pros: Easier to read and maintain than the inline approach, as it's defined in a separate function. * Cons: Requires an additional line of code for the `pack()` call. 3. **@Bavashi**: * Pros: Most intuitive and readable, as it uses recursion to traverse the array. * Cons: May be slower due to the overhead of recursive function calls. **Library Used** None of these approaches rely on any external libraries. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in these approaches. They all use standard JavaScript programming constructs, such as functions, arrays, and object properties. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few suggestions: * Use a library like [js-tree-data-structure](https://github.com/masylum/js-tree-data-structure) to build the tree. * Use a different data structure, such as an adjacency list or a linked list, instead of a traditional tree. * Experiment with parallel processing or concurrent execution to measure performance improvements. I hope this explanation helps!
Related benchmarks:
Array .push() vs .unshift(), 1M elements
arr unshift vs push + reverse (large array)
Speed of Arr.length and Slice
+ vs Number, with 100k numbers
Array last index
Comments
Confirm delete:
Do you really want to delete benchmark?