Skip to content

Commit

Permalink
fix(benchmarks): use correct usage of snabbdom API
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Oct 11, 2021
1 parent 3fbb4cf commit b2421d6
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 59 deletions.
7 changes: 6 additions & 1 deletion benchmarks/benchmark.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ export const removeKeys = (vnode) => {
}
};

export const clone = (vnode) => {
// Virtual DOM libraries like snabbdom, virtual-dom will
// mutate the vnode, even though the vnode must be
// immutable for the benchmark. For this case, we deep
// copy the vnode. This ensures compatibility throughout
// the suite tests.
export const vnodeAdapter = (vnode) => {
const clonedVNode = _.cloneDeep(vnode);
removeKeys(clonedVNode);
return clonedVNode;
Expand Down
13 changes: 7 additions & 6 deletions benchmarks/suites/appendManyRowsToLargeTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* @description appending 1,000 to a table of 10,000 rows.
*/

import { createElement, patch } from '../../src/index';
import { Suite, clone } from '../benchmark';
import { createElement, compose, childrenDriver } from '../../src/index';
import { Suite, vnodeAdapter } from '../benchmark';
import { buildData } from '../data';
import * as tiny_vdom from '../tiny-vdom';
import * as virtual_dom from 'virtual-dom';
import * as snabbdom from 'snabbdom';

const patch = compose([childrenDriver]);
const data = buildData(10000);
const createVNode = () => (
<table>
Expand All @@ -31,15 +32,15 @@ const suite = Suite('append many rows to large table (appending 1,000 to a table
patch(el(), vnode);
},
'tiny-vdom': () => {
tiny_vdom.patch(el(), clone(vnode), clone(oldVNode));
tiny_vdom.patch(el(), vnodeAdapter(vnode), vnodeAdapter(oldVNode));
},
'virtual-dom': () => {
const patches = virtual_dom.diff(clone(oldVNode), clone(vnode));
const patches = virtual_dom.diff(vnodeAdapter(oldVNode), vnodeAdapter(vnode));
virtual_dom.patch(el(), patches);
},
snabbdom: () => {
const patch = snabbdom.init([snabbdom.propsModule]);
patch(el(), clone(vnode));
const patch = snabbdom.init([]);
patch(snabbdom.toVNode(el()), vnodeAdapter(vnode));
},
DOM: () => {
const elClone = el();
Expand Down
13 changes: 7 additions & 6 deletions benchmarks/suites/clearRows.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* @description clearing a table with 1,000 rows
*/

import { createElement, patch } from '../../src/index';
import { Suite, clone } from '../benchmark';
import { createElement, compose, childrenDriver } from '../../src/index';
import { Suite, vnodeAdapter } from '../benchmark';
import { buildData } from '../data';
import * as tiny_vdom from '../tiny-vdom';
import * as virtual_dom from 'virtual-dom';
import * as snabbdom from 'snabbdom';

const patch = compose([childrenDriver]);
const data = buildData(1000);
const oldVNode = (
<table>
Expand All @@ -29,15 +30,15 @@ const suite = Suite('clear rows (clearing a table with 1,000 rows)', {
patch(el(), vnode);
},
'tiny-vdom': () => {
tiny_vdom.patch(el(), clone(vnode), clone(oldVNode));
tiny_vdom.patch(el(), vnodeAdapter(vnode), vnodeAdapter(oldVNode));
},
'virtual-dom': () => {
const patches = virtual_dom.diff(clone(oldVNode), clone(vnode));
const patches = virtual_dom.diff(vnodeAdapter(oldVNode), vnodeAdapter(vnode));
virtual_dom.patch(el(), patches);
},
snabbdom: () => {
const patch = snabbdom.init([snabbdom.propsModule]);
patch(el(), clone(vnode));
const patch = snabbdom.init([]);
patch(snabbdom.toVNode(el()), vnodeAdapter(vnode));
},
DOM: () => {
const elClone = el();
Expand Down
13 changes: 7 additions & 6 deletions benchmarks/suites/createManyRows.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* @description creating 10,000 rows
*/

import { createElement, patch } from '../../src/index';
import { Suite, clone } from '../benchmark';
import { createElement, compose, childrenDriver } from '../../src/index';
import { Suite, vnodeAdapter } from '../benchmark';
import { buildData } from '../data';
import * as tiny_vdom from '../tiny-vdom';
import * as virtual_dom from 'virtual-dom';
import * as snabbdom from 'snabbdom';

const patch = compose([childrenDriver]);
const data = buildData(10000);
const oldVNode = <table></table>;
const el = () => createElement(oldVNode);
Expand All @@ -29,15 +30,15 @@ const suite = Suite('create many rows (creating 10,000 rows)', {
patch(el(), vnode);
},
'tiny-vdom': () => {
tiny_vdom.patch(el(), clone(vnode), clone(oldVNode));
tiny_vdom.patch(el(), vnodeAdapter(vnode), vnodeAdapter(oldVNode));
},
'virtual-dom': () => {
const patches = virtual_dom.diff(clone(oldVNode), clone(vnode));
const patches = virtual_dom.diff(vnodeAdapter(oldVNode), vnodeAdapter(vnode));
virtual_dom.patch(el(), patches);
},
snabbdom: () => {
const patch = snabbdom.init([snabbdom.propsModule]);
patch(el(), clone(vnode));
const patch = snabbdom.init([]);
patch(snabbdom.toVNode(el()), vnodeAdapter(vnode));
},
DOM: () => {
const elClone = el();
Expand Down
13 changes: 7 additions & 6 deletions benchmarks/suites/createRows.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* @description creating 1,000 rows
*/

import { createElement, patch } from '../../src/index';
import { Suite, clone } from '../benchmark';
import { createElement, compose, childrenDriver } from '../../src/index';
import { Suite, vnodeAdapter } from '../benchmark';
import { buildData } from '../data';
import * as tiny_vdom from '../tiny-vdom';
import * as virtual_dom from 'virtual-dom';
import * as snabbdom from 'snabbdom';

const patch = compose([childrenDriver]);
const data = buildData(10000);
const oldVNode = <table></table>;
const el = () => createElement(oldVNode);
Expand All @@ -29,15 +30,15 @@ const suite = Suite('create rows (creating 1,000 rows)', {
patch(el(), vnode);
},
'tiny-vdom': () => {
tiny_vdom.patch(el(), clone(vnode), clone(oldVNode));
tiny_vdom.patch(el(), vnodeAdapter(vnode), vnodeAdapter(oldVNode));
},
'virtual-dom': () => {
const patches = virtual_dom.diff(clone(oldVNode), clone(vnode));
const patches = virtual_dom.diff(vnodeAdapter(oldVNode), vnodeAdapter(vnode));
virtual_dom.patch(el(), patches);
},
snabbdom: () => {
const patch = snabbdom.init([snabbdom.propsModule]);
patch(el(), clone(vnode));
const patch = snabbdom.init([]);
patch(snabbdom.toVNode(el()), vnodeAdapter(vnode));
},
DOM: () => {
const elClone = el();
Expand Down
13 changes: 7 additions & 6 deletions benchmarks/suites/partialUpdate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* @description updating every 10th row for 1,000 rows
*/

import { createElement, patch, UPDATE } from '../../src/index';
import { Suite, clone } from '../benchmark';
import { createElement, compose, childrenDriver, UPDATE } from '../../src/index';
import { Suite, vnodeAdapter } from '../benchmark';
import { buildData } from '../data';
import * as tiny_vdom from '../tiny-vdom';
import * as virtual_dom from 'virtual-dom';
import * as snabbdom from 'snabbdom';

const patch = compose([childrenDriver]);
const data = buildData(1000);
const oldVNode = (
<table>
Expand Down Expand Up @@ -44,15 +45,15 @@ const suite = Suite('partial update (updating every 10th row for 1,000 rows)', {
patch(el(), vnode);
},
'tiny-vdom': () => {
tiny_vdom.patch(el(), clone(vnode), clone(oldVNode));
tiny_vdom.patch(el(), vnodeAdapter(vnode), vnodeAdapter(oldVNode));
},
'virtual-dom': () => {
const patches = virtual_dom.diff(clone(oldVNode), clone(vnode));
const patches = virtual_dom.diff(vnodeAdapter(oldVNode), vnodeAdapter(vnode));
virtual_dom.patch(el(), patches);
},
snabbdom: () => {
const patch = snabbdom.init([snabbdom.propsModule]);
patch(el(), clone(vnode));
const patch = snabbdom.init([]);
patch(snabbdom.toVNode(el()), vnodeAdapter(vnode));
},
DOM: () => {
const elClone = el();
Expand Down
13 changes: 7 additions & 6 deletions benchmarks/suites/removeRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* @description removing one row
*/

import { createElement, DELETE, patch } from '../../src/index';
import { Suite, clone } from '../benchmark';
import { createElement, DELETE, compose, childrenDriver } from '../../src/index';
import { Suite, vnodeAdapter } from '../benchmark';
import { buildData } from '../data';
import * as tiny_vdom from '../tiny-vdom';
import * as virtual_dom from 'virtual-dom';
import * as snabbdom from 'snabbdom';

const patch = compose([childrenDriver]);
const data = buildData(1000);
const oldVNode = (
<table>
Expand Down Expand Up @@ -41,15 +42,15 @@ const suite = Suite('remove row (removing one row)', {
patch(el(), vnode);
},
'tiny-vdom': () => {
tiny_vdom.patch(el(), clone(vnode), clone(oldVNode));
tiny_vdom.patch(el(), vnodeAdapter(vnode), vnodeAdapter(oldVNode));
},
'virtual-dom': () => {
const patches = virtual_dom.diff(clone(oldVNode), clone(vnode));
const patches = virtual_dom.diff(vnodeAdapter(oldVNode), vnodeAdapter(vnode));
virtual_dom.patch(el(), patches);
},
snabbdom: () => {
const patch = snabbdom.init([snabbdom.propsModule]);
patch(el(), clone(vnode));
const patch = snabbdom.init([]);
patch(snabbdom.toVNode(el()), vnodeAdapter(vnode));
},
DOM: () => {
const elClone = el();
Expand Down
13 changes: 7 additions & 6 deletions benchmarks/suites/replaceAllRows.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* @description updating all 1,000 rows
*/

import { createElement, patch } from '../../src/index';
import { Suite, clone } from '../benchmark';
import { createElement, compose, childrenDriver } from '../../src/index';
import { Suite, vnodeAdapter } from '../benchmark';
import { buildData } from '../data';
import * as tiny_vdom from '../tiny-vdom';
import * as virtual_dom from 'virtual-dom';
import * as snabbdom from 'snabbdom';

const patch = compose([childrenDriver]);
const shuffleArray = (array) => {
for (
let i = array.length - 1 - Math.floor(Math.random() * (data.length / 3 + 1));
Expand Down Expand Up @@ -44,15 +45,15 @@ const suite = Suite('replace all rows (updating all 1,000 rows)', {
patch(el(), vnode);
},
'tiny-vdom': () => {
tiny_vdom.patch(el(), clone(vnode), clone(oldVNode));
tiny_vdom.patch(el(), vnodeAdapter(vnode), vnodeAdapter(oldVNode));
},
'virtual-dom': () => {
const patches = virtual_dom.diff(clone(oldVNode), clone(vnode));
const patches = virtual_dom.diff(vnodeAdapter(oldVNode), vnodeAdapter(vnode));
virtual_dom.patch(el(), patches);
},
snabbdom: () => {
const patch = snabbdom.init([snabbdom.propsModule]);
patch(el(), clone(vnode));
const patch = snabbdom.init([]);
patch(snabbdom.toVNode(el()), vnodeAdapter(vnode));
},
DOM: () => {
el().childNodes.forEach((tr, i) => {
Expand Down
13 changes: 7 additions & 6 deletions benchmarks/suites/selectRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* @description highlighting a selected row
*/

import { createElement, patch } from '../../src/index';
import { Suite, clone } from '../benchmark';
import { createElement, compose, childrenDriver } from '../../src/index';
import { Suite, vnodeAdapter } from '../benchmark';
import { buildData } from '../data';
import * as tiny_vdom from '../tiny-vdom';
import * as virtual_dom from 'virtual-dom';
import * as snabbdom from 'snabbdom';

const patch = compose([childrenDriver]);
const data = buildData(1000);
const createVNode = () => (
<table>
Expand All @@ -31,15 +32,15 @@ const suite = Suite('select row (highlighting a selected row)', {
patch(el(), vnode);
},
'tiny-vdom': () => {
tiny_vdom.patch(el(), clone(vnode), clone(oldVNode));
tiny_vdom.patch(el(), vnodeAdapter(vnode), vnodeAdapter(oldVNode));
},
'virtual-dom': () => {
const patches = virtual_dom.diff(clone(oldVNode), clone(vnode));
const patches = virtual_dom.diff(vnodeAdapter(oldVNode), vnodeAdapter(vnode));
virtual_dom.patch(el(), patches);
},
snabbdom: () => {
const patch = snabbdom.init([snabbdom.propsModule]);
patch(el(), clone(vnode));
const patch = snabbdom.init([]);
patch(snabbdom.toVNode(el()), vnodeAdapter(vnode));
},
DOM: () => {
el().childNodes[row].style.background = 'red';
Expand Down
13 changes: 7 additions & 6 deletions benchmarks/suites/swapRows.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* @description swap 2 rows for table with 1,000 rows
*/

import { createElement, patch, UPDATE } from '../../src/index';
import { Suite, clone } from '../benchmark';
import { createElement, compose, childrenDriver, UPDATE } from '../../src/index';
import { Suite, vnodeAdapter } from '../benchmark';
import { buildData } from '../data';
import * as tiny_vdom from '../tiny-vdom';
import * as virtual_dom from 'virtual-dom';
import * as snabbdom from 'snabbdom';

const patch = compose([childrenDriver]);
const data = buildData(1000);
const oldVNode = (
<table>
Expand Down Expand Up @@ -45,15 +46,15 @@ const suite = Suite('swap rows (swap 2 rows for table with 1,000 rows)', {
patch(el(), vnode);
},
'tiny-vdom': () => {
tiny_vdom.patch(el(), clone(vnode), clone(oldVNode));
tiny_vdom.patch(el(), vnodeAdapter(vnode), vnodeAdapter(oldVNode));
},
'virtual-dom': () => {
const patches = virtual_dom.diff(clone(oldVNode), clone(vnode));
const patches = virtual_dom.diff(vnodeAdapter(oldVNode), vnodeAdapter(vnode));
virtual_dom.patch(el(), patches);
},
snabbdom: () => {
const patch = snabbdom.init([snabbdom.propsModule]);
patch(el(), clone(vnode));
const patch = snabbdom.init([]);
patch(snabbdom.toVNode(el()), vnodeAdapter(vnode));
},
DOM: () => {
const elClone = el();
Expand Down
6 changes: 2 additions & 4 deletions src/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export const flushWorkStack = (
workStack: VTask[],
commit: VCommit = (task: VTask): void => task(),
): void => {
for (let i = 0; i < workStack.length; ++i) {
commit(workStack[i]);
}
for (let i = 0; i < workStack.length; ++i) commit(workStack[i]);
};

/**
Expand Down Expand Up @@ -76,4 +74,4 @@ export const compose =
/**
* Diffs two VNodes and modifies the DOM node based on the necessary changes
*/
export const patch = compose([propsDriver, childrenDriver]);
export const patch = compose([childrenDriver, propsDriver]);

0 comments on commit b2421d6

Please sign in to comment.