Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TestsPerf
(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 break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark measures the performance of three different functions that create a tree data structure from an array of objects. The input array is populated with 1000 objects, each containing `id`, `name`, and `parent` properties. **Script Preparation Code** The script preparation code initializes an empty array `arr` and sets its length to 1000. It also defines a function `init()` that populates the array with 1000 objects using a loop. The `init()` function is not called in this benchmark, but it's likely used by the test cases. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark only measures the execution time of the JavaScript functions. **Test Cases** The benchmark consists of three test cases: 1. **@john36allTa**: This test case uses a recursive function `pack()` that traverses the array and builds a tree data structure. 2. **@dimoff66**: This test case uses an iterative approach to build the same tree data structure, using `Object.assign()` and `map()` functions. 3. **@Bavashi**: This test case uses a hybrid approach that combines recursive and iterative methods to build the tree data structure. **Library and Dependencies** None of the test cases rely on any external libraries or dependencies. The code is self-contained and only uses built-in JavaScript features. **Special Features or Syntax** There are no special JavaScript features or syntax used in these benchmark test cases. They only demonstrate basic programming concepts, such as array manipulation, function definitions, and loop control. **Performance Considerations** The performance of the three test cases can be compared based on their execution time per second: * **@john36allTa**: 191.7964630126953 executions/second * **@dimoff66**: 116.13249969482422 executions/second * **@Bavashi**: 90.94884490966797 executions/second The results suggest that the recursive approach in `@john36allTa` is the fastest, followed by the iterative approach in `@dimoff66`, and then the hybrid approach in `@Bavashi`. **Other Alternatives** If you were to rewrite these test cases using alternative approaches, some options could be: * Using a library like [binary-search-tree](https://github.com/mishoo/UglifyJS/tree/master/lib/binary-search-tree) for building tree data structures. * Employing parallel processing or multi-threading techniques to speed up the execution time. * Utilizing just-in-time (JIT) compilation or dynamic compilation methods, such as [ES6 modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). Please note that these alternatives would likely introduce additional complexity and may not always result in improved performance.
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?