Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
TestPerf
(version: 0)
Comparing performance of:
1 vs 2 vs 3
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:
1
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)
2
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)
3
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
1
2
3
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!
Related benchmarks:
push VS destructuration
slice vs new allocation1
Speed of Arr.length and Slice
+ vs Number, with 100k numbers
Array Slicing
Comments
Confirm delete:
Do you really want to delete benchmark?