var arr = [];
var len = 1000;
function init() {
for (var i = 0; i < len; i++) {
arr.push({
id: i+1,
name: i+" ...",
parent: i
});
}}
init();
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)
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)
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);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
@john36allTa | |
@dimoff66 | |
@Bavashi |
Test name | Executions per second |
---|---|
@john36allTa | 191.8 Ops/sec |
@dimoff66 | 116.1 Ops/sec |
@Bavashi | 90.9 Ops/sec |
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:
pack()
that traverses the array and builds a tree data structure.Object.assign()
and map()
functions.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:
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:
Please note that these alternatives would likely introduce additional complexity and may not always result in improved performance.