Skip to content

Commit 5c57c25

Browse files
committed
benchmark diffing with nested trees
1 parent f380fd5 commit 5c57c25

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

benchmarks/diff.bench.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createElement } from '../src/createElement';
2+
import { diff } from '../src/diff';
3+
4+
function buildTree(depth: number, breadth: number): any {
5+
if (depth === 0) return createElement('span', {}, ['leaf']);
6+
const children = Array.from({ length: breadth }, () => buildTree(depth - 1, breadth));
7+
return createElement('div', { className: 'l' + depth }, children);
8+
}
9+
10+
function bench(name: string, fn: () => void, n = 1000) {
11+
const start = performance.now();
12+
for (let i = 0; i < n; i++) fn();
13+
const ms = ((performance.now() - start) / n).toFixed(3);
14+
console.log(name + ': ' + ms + 'ms avg (' + n + ' runs)');
15+
}
16+
17+
const a = buildTree(3, 3);
18+
const b = buildTree(3, 3);
19+
bench('diff small (3x3)', () => diff(a, b));
20+
21+
const ma = buildTree(4, 4);
22+
const mb = buildTree(4, 4);
23+
bench('diff medium (4x4)', () => diff(ma, mb));

0 commit comments

Comments
 (0)