fast-totree

1.2.3 • Public • Published

fast-totree

Installation

$ npm install fast-totree --save

Usage

 
require('fast-totree');
 
let list = [
 
  {id:1,name:'max1',parentId:0},
  {id:2,name:'max2',parentId:0},
  {id:3,name:'max3',parentId:1},
  {id:4,name:'max4',parentId:1},
  {id:5,name:'max5',parentId:2},
];
 
//如果数据格式与上述一致,比较键为id、parentId
let tree = list.toTree();
 
//如果不一致,则重置配置文件,示例数据格式 {_id:1,name:'max',_pid:0}
const config = {
 
    key:'_id',
    pid:'_pid',        
    chidren:'_chidren',
    value:'0'
}
 
let tree2 = list.toTree(config);
 
/*
 * Output:
 *
 * item 0
 *   item 2 
 *   item 3
 * item 1
 *   item 4
 */

性能测试

name 1千条 1万条 10万条 50万条
fast-totree 1ms 15ms 95ms 450ms
array-to-tree 9ms 溢出 溢出 溢出
totree 2ms 溢出 溢出 溢出
performant-array-to-tree 1ms 10ms 103ms 500ms
list-to-tree 26ms 溢出 溢出 溢出

Package Sidebar

Install

npm i fast-totree

Weekly Downloads

16

Version

1.2.3

License

MIT

Unpacked Size

3.53 kB

Total Files

4

Last publish

Collaborators

  • leastcoding