Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TestsPerf21
(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 res1 = 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 res2 = 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 res3 = 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** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents two benchmark definitions for three different test cases. **Script Preparation Code** The script preparation code is used to prepare the input data for each benchmark. In this case, the input data is an array `arr` of objects with `id`, `name`, and `parent` properties. Each object in the array has a unique `id` between 1 and 1000, and `parent` points to another object's `id`. The purpose of this code is to generate a hierarchical tree structure from the input data. **Html Preparation Code** There is no HTML preparation code provided for these benchmarks. **Benchmark Definitions** Each benchmark definition is a JavaScript function that takes the prepared input data as an argument and returns the resulting tree structure. There are three test cases: 1. `pack` by `@john36allTa`: This function uses the `filter`, `map`, and `reduce` methods to create the tree structure. 2. `pack` by `@dimoff66`: This function uses `Object.assign` and `map` to create the tree structure. 3. `makeTree` by `@Bavashi`: This function recursively builds the tree structure using a simple loop. **Comparison of Options** The three benchmark definitions use different approaches to build the tree structure: * `pack` by `@john36allTa` uses a declarative style, relying on the built-in methods of the Array prototype. * `pack` by `@dimoff66` uses an imperative style, using explicit loops and object manipulation. * `makeTree` by `@Bavashi` uses a recursive approach, with a simple loop that calls itself to build the tree structure. **Pros and Cons** Here are some pros and cons of each approach: * Declarative style (`pack` by `@john36allTa`): + Pros: concise, readable, and efficient. + Cons: may be less intuitive for beginners, and may not perform well with large inputs. * Imperative style (`pack` by `@dimoff66`): + Pros: explicit control over the loop, and may be faster for small inputs. + Cons: more verbose, and may be slower for large inputs due to object creation overhead. * Recursive approach (`makeTree` by `@Bavashi`): + Pros: easy to understand, and can be efficient with careful optimization. + Cons: may lead to stack overflow errors for very deep trees, and can be less readable. **Library Usage** None of the benchmark definitions use any external libraries. However, they do rely on built-in JavaScript methods and syntax. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in these benchmark definitions. **Other Alternatives** Some alternative approaches to building tree structures could include: * Using a library like D3.js for efficient and flexible graph rendering. * Implementing a custom binary tree data structure using arrays and pointers. * Using a recursive function with memoization to avoid redundant computations. However, the built-in methods of the Array prototype (used in `pack` by `@john36allTa`) provide an efficient and concise way to build tree structures for many use cases.
Related benchmarks:
Array clone
array push
slice vs new allocation1
Speed of Arr.length and Slice
+ vs Number, with 100k numbers
Comments
Confirm delete:
Do you really want to delete benchmark?