|
| 1 | +// Benchmark: DFS and BFS Pre-Order Traversal Comparison |
| 2 | +// Compares js-tuple NestedMap, tree-model, bintrees, js-tree, binary-search-tree |
| 3 | + |
| 4 | +const Benchmark = require('benchmark'); |
| 5 | +const TreeModel = require('tree-model'); |
| 6 | +const { RBTree } = require('bintrees'); |
| 7 | +const JSTree = require('js-tree'); |
| 8 | +const { NestedMap, TraverseMode, YieldMode } = require('../dist/min'); |
| 9 | + |
| 10 | +const SIZES = [100, 500, 1000, 2000, 5000]; |
| 11 | +const MODES = [ |
| 12 | + { name: 'DFS Pre-Order', traverseMode: 'dfs', order: 'pre' }, |
| 13 | + { name: 'DFS Post-Order', traverseMode: 'dfs', order: 'post' }, |
| 14 | + { name: 'BFS Pre-Order', traverseMode: 'bfs', order: 'pre' }, |
| 15 | + { name: 'BFS Post-Order', traverseMode: 'bfs', order: 'post' }, |
| 16 | +]; |
| 17 | + |
| 18 | +function buildJsTupleTree(NODE_COUNT) { |
| 19 | + const map = new NestedMap(); |
| 20 | + for (let i = 0; i < NODE_COUNT; ++i) { |
| 21 | + map.set([i], i); |
| 22 | + } |
| 23 | + return map; |
| 24 | +} |
| 25 | + |
| 26 | +function buildTreeModelTree(NODE_COUNT) { |
| 27 | + const tree = new TreeModel(); |
| 28 | + let root = tree.parse({ id: 0, children: [] }); |
| 29 | + let current = root; |
| 30 | + for (let i = 1; i < NODE_COUNT; ++i) { |
| 31 | + const child = tree.parse({ id: i, children: [] }); |
| 32 | + current.addChild(child); |
| 33 | + current = child; |
| 34 | + } |
| 35 | + return root; |
| 36 | +} |
| 37 | + |
| 38 | +function buildBintree(NODE_COUNT) { |
| 39 | + const tree = new RBTree((a, b) => a - b); |
| 40 | + for (let i = 0; i < NODE_COUNT; ++i) { |
| 41 | + tree.insert(i); |
| 42 | + } |
| 43 | + return tree; |
| 44 | +} |
| 45 | + |
| 46 | +function buildJsTree(NODE_COUNT) { |
| 47 | + let rootObj = { id: 0, children: [] }; |
| 48 | + let current = rootObj; |
| 49 | + for (let i = 1; i < NODE_COUNT; ++i) { |
| 50 | + const child = { id: i, children: [] }; |
| 51 | + current.children.push(child); |
| 52 | + current = child; |
| 53 | + } |
| 54 | + return new JSTree(rootObj); |
| 55 | +} |
| 56 | + |
| 57 | +function dfsPreTreeModel(root, NODE_COUNT) { |
| 58 | + let count = 0; |
| 59 | + root.walk(() => { |
| 60 | + count++; |
| 61 | + }); |
| 62 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 63 | +} |
| 64 | + |
| 65 | +function dfsPostTreeModel(root, NODE_COUNT) { |
| 66 | + let count = 0; |
| 67 | + function walk(node) { |
| 68 | + if (node.children) { |
| 69 | + for (const child of node.children) { |
| 70 | + walk(child); |
| 71 | + } |
| 72 | + } |
| 73 | + count++; |
| 74 | + } |
| 75 | + walk(root); |
| 76 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 77 | +} |
| 78 | + |
| 79 | +function bfsPreTreeModel(root, NODE_COUNT) { |
| 80 | + let count = 0; |
| 81 | + const queue = [root]; |
| 82 | + while (queue.length) { |
| 83 | + const node = queue.shift(); |
| 84 | + count++; |
| 85 | + if (node.children) { |
| 86 | + for (const child of node.children) { |
| 87 | + queue.push(child); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 92 | +} |
| 93 | + |
| 94 | +function bfsPostTreeModel(root, NODE_COUNT) { |
| 95 | + let count = 0; |
| 96 | + function bfsPost(nodes) { |
| 97 | + const nextLevel = []; |
| 98 | + for (const node of nodes) { |
| 99 | + if (node.children) { |
| 100 | + for (const child of node.children) { |
| 101 | + nextLevel.push(child); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + if (nextLevel.length) bfsPost(nextLevel); |
| 106 | + for (const node of nodes) { |
| 107 | + count++; |
| 108 | + } |
| 109 | + } |
| 110 | + bfsPost([root]); |
| 111 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 112 | +} |
| 113 | + |
| 114 | +function dfsPreBintree(tree, NODE_COUNT) { |
| 115 | + let count = 0; |
| 116 | + tree.each(() => { |
| 117 | + count++; |
| 118 | + }); |
| 119 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 120 | +} |
| 121 | + |
| 122 | +function dfsPostBintree(tree, NODE_COUNT) { |
| 123 | + let count = 0; |
| 124 | + function walk(node) { |
| 125 | + if (!node) return; |
| 126 | + walk(node.left); |
| 127 | + walk(node.right); |
| 128 | + if (node.hasOwnProperty('key')) count++; |
| 129 | + } |
| 130 | + walk(tree._root); |
| 131 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 132 | +} |
| 133 | + |
| 134 | +function bfsPreBintree(tree, NODE_COUNT) { |
| 135 | + throw new Error('BFS not supported for RBTree'); |
| 136 | +} |
| 137 | + |
| 138 | +function bfsPostBintree(tree, NODE_COUNT) { |
| 139 | + throw new Error('BFS not supported for RBTree'); |
| 140 | +} |
| 141 | + |
| 142 | +function dfsPreJsTree(tree, NODE_COUNT) { |
| 143 | + let count = 0; |
| 144 | + function walk(node) { |
| 145 | + count++; |
| 146 | + if (node.children) { |
| 147 | + for (const child of node.children) { |
| 148 | + walk(child); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + walk(tree.obj); |
| 153 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 154 | +} |
| 155 | + |
| 156 | +function dfsPostJsTree(tree, NODE_COUNT) { |
| 157 | + let count = 0; |
| 158 | + function walk(node) { |
| 159 | + if (node.children) { |
| 160 | + for (const child of node.children) { |
| 161 | + walk(child); |
| 162 | + } |
| 163 | + } |
| 164 | + count++; |
| 165 | + } |
| 166 | + walk(tree.obj); |
| 167 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 168 | +} |
| 169 | + |
| 170 | +function bfsPreJsTree(tree, NODE_COUNT) { |
| 171 | + let count = 0; |
| 172 | + const queue = [tree.obj]; |
| 173 | + while (queue.length) { |
| 174 | + const node = queue.shift(); |
| 175 | + count++; |
| 176 | + if (node.children) { |
| 177 | + for (const child of node.children) { |
| 178 | + queue.push(child); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 183 | +} |
| 184 | + |
| 185 | +function bfsPostJsTree(tree, NODE_COUNT) { |
| 186 | + let count = 0; |
| 187 | + function bfsPost(nodes) { |
| 188 | + const nextLevel = []; |
| 189 | + for (const node of nodes) { |
| 190 | + if (node.children) { |
| 191 | + for (const child of node.children) { |
| 192 | + nextLevel.push(child); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + if (nextLevel.length) bfsPost(nextLevel); |
| 197 | + for (const node of nodes) { |
| 198 | + count++; |
| 199 | + } |
| 200 | + } |
| 201 | + bfsPost([tree.obj]); |
| 202 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 203 | +} |
| 204 | + |
| 205 | +function runSuite(NODE_COUNT) { |
| 206 | + for (const mode of MODES) { |
| 207 | + console.log(`\n${mode.name} Benchmark (${NODE_COUNT} nodes)\n`); |
| 208 | + const suite = new Benchmark.Suite(); |
| 209 | + const failedLibs = []; |
| 210 | + |
| 211 | + suite |
| 212 | + .add('js-tuple NestedMap', function () { |
| 213 | + const map = buildJsTupleTree(NODE_COUNT); |
| 214 | + let count = 0; |
| 215 | + let traverseMode, yieldMode; |
| 216 | + if (mode.traverseMode === 'dfs') { |
| 217 | + traverseMode = TraverseMode.DepthFirst; |
| 218 | + } else { |
| 219 | + traverseMode = TraverseMode.BreadthFirst; |
| 220 | + } |
| 221 | + if (mode.order === 'pre') { |
| 222 | + yieldMode = YieldMode.PreOrder; |
| 223 | + } else { |
| 224 | + yieldMode = YieldMode.PostOrder; |
| 225 | + } |
| 226 | + for (const value of map.values({ traverseMode, yieldMode })) { |
| 227 | + count++; |
| 228 | + } |
| 229 | + if (count !== NODE_COUNT) throw new Error('Incorrect node count'); |
| 230 | + }) |
| 231 | + .add('tree-model', function () { |
| 232 | + const root = buildTreeModelTree(NODE_COUNT); |
| 233 | + if (mode.traverseMode === 'dfs' && mode.order === 'pre') |
| 234 | + dfsPreTreeModel(root, NODE_COUNT); |
| 235 | + else if (mode.traverseMode === 'dfs' && mode.order === 'post') |
| 236 | + dfsPostTreeModel(root, NODE_COUNT); |
| 237 | + else if (mode.traverseMode === 'bfs' && mode.order === 'pre') |
| 238 | + bfsPreTreeModel(root, NODE_COUNT); |
| 239 | + else if (mode.traverseMode === 'bfs' && mode.order === 'post') |
| 240 | + bfsPostTreeModel(root, NODE_COUNT); |
| 241 | + }) |
| 242 | + .add('bintrees RBTree', function () { |
| 243 | + const tree = buildBintree(NODE_COUNT); |
| 244 | + if (mode.traverseMode === 'dfs' && mode.order === 'pre') |
| 245 | + dfsPreBintree(tree, NODE_COUNT); |
| 246 | + else if (mode.traverseMode === 'dfs' && mode.order === 'post') |
| 247 | + dfsPostBintree(tree, NODE_COUNT); |
| 248 | + else if (mode.traverseMode === 'bfs' && mode.order === 'pre') |
| 249 | + bfsPreBintree(tree, NODE_COUNT); |
| 250 | + else if (mode.traverseMode === 'bfs' && mode.order === 'post') |
| 251 | + bfsPostBintree(tree, NODE_COUNT); |
| 252 | + }) |
| 253 | + .add('js-tree', function () { |
| 254 | + const tree = buildJsTree(NODE_COUNT); |
| 255 | + if (mode.traverseMode === 'dfs' && mode.order === 'pre') |
| 256 | + dfsPreJsTree(tree, NODE_COUNT); |
| 257 | + else if (mode.traverseMode === 'dfs' && mode.order === 'post') |
| 258 | + dfsPostJsTree(tree, NODE_COUNT); |
| 259 | + else if (mode.traverseMode === 'bfs' && mode.order === 'pre') |
| 260 | + bfsPreJsTree(tree, NODE_COUNT); |
| 261 | + else if (mode.traverseMode === 'bfs' && mode.order === 'post') |
| 262 | + bfsPostJsTree(tree, NODE_COUNT); |
| 263 | + }) |
| 264 | + .on('cycle', function (event) { |
| 265 | + console.log(String(event.target)); |
| 266 | + }) |
| 267 | + .on('error', function (event) { |
| 268 | + console.log( |
| 269 | + `FAILED: ${event.target.name} (${event.target.error && event.target.error.message})`, |
| 270 | + ); |
| 271 | + failedLibs.push(event.target.name); |
| 272 | + }) |
| 273 | + .on('complete', function () { |
| 274 | + if (failedLibs.length) { |
| 275 | + console.log( |
| 276 | + `\nLibraries that failed for ${mode.name} ${NODE_COUNT} nodes: ${failedLibs.join(', ')}`, |
| 277 | + ); |
| 278 | + } |
| 279 | + console.log( |
| 280 | + `\nFastest for ${mode.name}: ` + this.filter('fastest').map('name'), |
| 281 | + ); |
| 282 | + console.log('\n' + '='.repeat(80) + '\n'); |
| 283 | + console.log(`${mode.name} Benchmark complete!`); |
| 284 | + }) |
| 285 | + .run({ async: false }); |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +for (const size of SIZES) { |
| 290 | + runSuite(size); |
| 291 | +} |
0 commit comments