Skip to content

Commit 0541365

Browse files
committed
add vanillajs-lite impl.
1 parent c5660d9 commit 0541365

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>VanillaJS-Lite-"keyed"</title>
6+
<link href="/css/currentStyle.css" rel="stylesheet"/>
7+
</head>
8+
<body>
9+
<div id="main">
10+
<div class="container">
11+
<div class="jumbotron">
12+
<div class="row">
13+
<div class="col-md-6">
14+
<h1>VanillaJS-Lite-"keyed"</h1>
15+
</div>
16+
<div class="col-md-6">
17+
<div class="row">
18+
<div class="col-sm-6 smallpad">
19+
<button type="button" class="btn btn-primary btn-block" id="run">Create 1,000 rows</button>
20+
</div>
21+
<div class="col-sm-6 smallpad">
22+
<button type="button" class="btn btn-primary btn-block" id="runlots">Create 10,000 rows</button>
23+
</div>
24+
<div class="col-sm-6 smallpad">
25+
<button type="button" class="btn btn-primary btn-block" id="add">Append 1,000 rows</button>
26+
</div>
27+
<div class="col-sm-6 smallpad">
28+
<button type="button" class="btn btn-primary btn-block" id="update">Update every 10th row</button>
29+
</div>
30+
<div class="col-sm-6 smallpad">
31+
<button type="button" class="btn btn-primary btn-block" id="clear">Clear</button>
32+
</div>
33+
<div class="col-sm-6 smallpad">
34+
<button type="button" class="btn btn-primary btn-block" id="swaprows">Swap Rows</button>
35+
</div>
36+
</div>
37+
</div>
38+
</div>
39+
</div>
40+
<table class="table table-hover table-striped test-data">
41+
<tbody id="tbody">
42+
</tbody>
43+
</table>
44+
<span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span>
45+
</div>
46+
</div>
47+
<template id="rowTemplate"><tr><td class="col-md-1">?</td><td class="col-md-4"><a>?</a></td><td class="col-md-1"><a><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td><td class="col-md-6"></td></tr></template>
48+
<script src="src/Main.js" type="module"></script>
49+
</body>
50+
</html>

frameworks/keyed/vanillajs-lite/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"private": true,
3+
"name": "js-framework-benchmark-vanillajs-lite",
4+
"version": "1.0.0",
5+
"description": "Vanilla.JS Lite Demo",
6+
"js-framework-benchmark": {
7+
"frameworkVersion": "",
8+
"frameworkHomeURL": "",
9+
"issues": [772]
10+
},
11+
"scripts": {
12+
"dev": "exit 0",
13+
"build-prod": "exit 0"
14+
},
15+
"devDependencies": {}
16+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const adjectives = ['pretty', 'large', 'big', 'small', 'tall', 'short', 'long', 'handsome', 'plain', 'quaint', 'clean', 'elegant', 'easy', 'angry', 'crazy', 'helpful', 'mushy', 'odd', 'unsightly', 'adorable', 'important', 'inexpensive', 'cheap', 'expensive', 'fancy'];
2+
const colours = ['red', 'yellow', 'blue', 'green', 'pink', 'brown', 'purple', 'brown', 'white', 'black', 'orange'];
3+
const nouns = ['table', 'chair', 'house', 'bbq', 'desk', 'car', 'pony', 'cookie', 'sandwich', 'burger', 'pizza', 'mouse', 'keyboard'];
4+
5+
const pick = dict => dict[Math.round(Math.random() * 1000) % dict.length];
6+
7+
let ID = 1, rows = [], selection;
8+
const ROW = Symbol(), ACTION = Symbol();
9+
10+
const rowTemplate = document.querySelector('#rowTemplate').content.firstChild;
11+
const table = document.querySelector('table');
12+
let tbody = document.querySelector('tbody');
13+
14+
const {cloneNode, insertBefore} = Node.prototype;
15+
const clone = (cloneNode.bind(rowTemplate, true));
16+
const insert = ((row, before = null) => insertBefore.call(tbody, row, before));
17+
18+
const build = (() => {
19+
const tr = clone();
20+
const td1 = tr.firstChild, td2 = td1.nextSibling, td3 = td2.nextSibling;
21+
const a1 = td2.firstChild, a2 = td3.firstChild;
22+
const label = `${pick(adjectives)} ${pick(colours)} ${pick(nouns)}`;
23+
td1.firstChild.nodeValue = ID++;
24+
(tr.label = a1.firstChild).nodeValue = label;
25+
a1[ACTION] = select, a2[ACTION] = remove;
26+
return insert(a1[ROW] = a2[ROW] = tr);
27+
});
28+
29+
const create = count => [...Array(count)].map(build);
30+
31+
const select = (set => row => {
32+
set('remove'), selection = row, set('add');
33+
})(setter => selection?.classList[setter]('danger'));
34+
35+
const remove = (match => row => {
36+
rows = rows.filter(match, row), row.remove();
37+
})(function (row) { return row !== this; });
38+
39+
const clear = () => {
40+
rows = [], selection = null;
41+
const clone = tbody.cloneNode();
42+
tbody.remove(), insertBefore.call(table, tbody = clone, null);
43+
};
44+
45+
document.querySelectorAll('button').forEach(function (button) {
46+
button.addEventListener('click', this[button.id]);
47+
}, {
48+
run () { clear(), rows = create(1000); },
49+
runlots () { clear(), rows = create(10000); },
50+
add () { rows = [...rows, ...create(1000)]; },
51+
clear,
52+
update () {
53+
for (let i = 0; i < rows.length; i += 10)
54+
rows[i].label.nodeValue += ' !!!';
55+
},
56+
swaprows () {
57+
if (rows.length > 998)
58+
insert(rows[1], rows[998]), insert(rows[998], rows[2]),
59+
[rows[1], rows[998]] = [rows[998], rows[1]];
60+
}
61+
});
62+
63+
table.addEventListener('click', e => {
64+
let {target: t} = e;
65+
e.stopPropagation(), (t[ACTION] ?? (t = t.parentNode)[ACTION])?.(t[ROW]);
66+
});
67+

0 commit comments

Comments
 (0)