Skip to content

Simplify lit-html benchmark code #521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 27 additions & 33 deletions frameworks/keyed/lit-html/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { html, render } from '../node_modules/lit-html/lit-html.js';
import { repeat } from '../node_modules/lit-html/directives/repeat.js';
import { guard } from '../node_modules/lit-html/directives/guard.js';

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'];
Expand All @@ -9,7 +8,7 @@ const nouns = ['table', 'chair', 'house', 'bbq', 'desk', 'car', 'pony', 'cookie'

let data = [];
let did = 1;
let selected = -1;
let selectedRow;

const add = () => {
data = data.concat(buildData(1000));
Expand All @@ -28,53 +27,49 @@ const clear = () => {
_render();
};
const interact = e => {
const interaction = e.target.getAttribute('data-interaction');
const id = parseInt(
e.target.parentNode.id ||
e.target.parentNode.parentNode.id ||
e.target.parentNode.parentNode.parentNode.id
);
const td = e.target.closest('td');
const tr = td.parentNode;
const id = parseInt(tr.id);
const interaction = td.getAttribute('data-interaction');
if (interaction === 'delete') {
del(id)
del(id);
} else {
select(id)
select(tr);
}
};
const del = id => {
const idx = data.findIndex(d => d.id === id);
data.splice(idx, 1)
data.splice(idx, 1);
_render();
};
const select = id => {
if (selected > -1) {
data[selected] = { ...data[selected], selected: false }
const select = row => {
row.classList.add('danger');
if (selectedRow !== undefined) {
selectedRow.classList.remove('danger');
}
selected = data.findIndex(d => d.id === id);
data[selected] = { ...data[selected], selected: true }
_render();
selectedRow = row;
};
const swapRows = () => {
if (data.length > 998) {
const tmp = data[1]
data[1] = data[998]
data[998] = tmp
const tmp = data[1];
data[1] = data[998];
data[998] = tmp;
}
_render();
};
const update = () => {
for(let i = 0; i < data.length; i += 10) {
const item = data[i]
data[i] = { ...item, label: item.label + ' !!!' }
for (let i = 0; i < data.length; i += 10) {
const item = data[i];
data[i].label += ' !!!';
}
_render();
};
const buildData = count => {
const data = [];
for (var i = 0; i < count; i++) {
for (let i = 0; i < count; i++) {
data.push({
id: did++,
label: `${adjectives[_random(adjectives.length)]} ${colours[_random(colours.length)]} ${nouns[_random(nouns.length)]}`,
selected: false,
});
}
return data;
Expand Down Expand Up @@ -126,20 +121,19 @@ const template = () => html`
<table @click=${interact} class="table table-hover table-striped test-data">
<tbody>${repeat(data,
item => item.id,
item => guard(item, () => html`
<tr id=${item.id} class=${item.selected ? 'danger' : ''}>
item => html`
<tr id=${item.id}>
<td class="col-md-1">${item.id}</td>
<td data-interaction='select' class="col-md-4">
<a data-interaction='select'>${item.label}</a>
<td class="col-md-4">
<a>${item.label}</a>
</td>
<td data-interaction='delete' class="col-md-1">
<a data-interaction='delete'>
<span data-interaction='delete' class="glyphicon glyphicon-remove" aria-hidden="true"></span>
<a>
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</a>
</td>
<td class="col-md-6"></td>
</tr>`)
)}
</tr>`)}
</tbody>
</table>
<span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span>
Expand Down
58 changes: 26 additions & 32 deletions frameworks/non-keyed/lit-html/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { html, render } from '../node_modules/lit-html/lit-html.js';
import { guard } from '../node_modules/lit-html/directives/guard.js';

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'];
Expand All @@ -8,7 +7,7 @@ const nouns = ['table', 'chair', 'house', 'bbq', 'desk', 'car', 'pony', 'cookie'

let data = [];
let did = 1;
let selected = -1;
let selectedRow;

const add = () => {
data = data.concat(buildData(1000));
Expand All @@ -27,43 +26,40 @@ const clear = () => {
_render();
};
const interact = e => {
const interaction = e.target.getAttribute('data-interaction');
const id = parseInt(
e.target.parentNode.id ||
e.target.parentNode.parentNode.id ||
e.target.parentNode.parentNode.parentNode.id
);
const td = e.target.closest('td');
const tr = td.parentNode;
const id = parseInt(tr.id);
const interaction = td.getAttribute('data-interaction');
if (interaction === 'delete') {
del(id)
del(id);
} else {
select(id)
select(tr);
}
};
const del = id => {
const idx = data.findIndex(d => d.id === id);
data.splice(idx, 1)
data.splice(idx, 1);
_render();
};
const select = id => {
if (selected > -1) {
data[selected] = { ...data[selected], selected: false }
const select = row => {
row.classList.add('danger');
if (selectedRow !== undefined) {
selectedRow.classList.remove('danger');
}
selected = data.findIndex(d => d.id === id);
data[selected] = { ...data[selected], selected: true }
_render();
selectedRow = row;
};
const swapRows = () => {
if (data.length > 998) {
const tmp = data[1]
data[1] = data[998]
data[998] = tmp
const tmp = data[1];
data[1] = data[998];
data[998] = tmp;
}
_render();
};
const update = () => {
for(let i = 0; i < data.length; i += 10) {
const item = data[i]
data[i] = { ...item, label: item.label + ' !!!' }
for (let i = 0; i < data.length; i += 10) {
const item = data[i];
data[i].label += ' !!!';
}
_render();
};
Expand All @@ -73,7 +69,6 @@ const buildData = count => {
data.push({
id: did++,
label: `${adjectives[_random(adjectives.length)]} ${colours[_random(colours.length)]} ${nouns[_random(nouns.length)]}`,
selected: false,
});
}
return data;
Expand Down Expand Up @@ -123,20 +118,19 @@ const template = () => html`
</div>
</div>
<table @click=${interact} class="table table-hover table-striped test-data">
<tbody>${data.map(item => guard(item, () => html`
<tr id=${item.id} class=${item.selected ? 'danger' : ''}>
<tbody>${data.map(item => html`
<tr id=${item.id}>
<td class="col-md-1">${item.id}</td>
<td data-interaction='select' class="col-md-4">
<a data-interaction='select'>${item.label}</a>
<td class="col-md-4">
<a>${item.label}</a>
</td>
<td data-interaction='delete' class="col-md-1">
<a data-interaction='delete'>
<span data-interaction='delete' class="glyphicon glyphicon-remove" aria-hidden="true"></span>
<a>
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</a>
</td>
<td class="col-md-6"></td>
</tr>`)
)}
</tr>`)}
</tbody>
</table>
<span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span>
Expand Down