Skip to content

Instantly share code, notes, and snippets.

@lega911
Last active September 19, 2020 22:29
Show Gist options
  • Save lega911/4372e9eabb3bba29057be0f7766264a9 to your computer and use it in GitHub Desktop.
Save lega911/4372e9eabb3bba29057be0f7766264a9 to your computer and use it in GitHub Desktop.
Small benchmark
<script>
let todos = [];
let duration = '';
function timeit(fn) {
let start = Date.now();
fn();
setTimeout(() => {
duration += ' + ' + (Date.now() - start);
}, 10);
};
function test() {
timeit(() => {
todos.length = 0;
for(let i=0;i<5000;i++) {
todos.push({
name: '' + i,
done: false
});
}
});
}
function remove(todo) {
timeit(() => {
todos.splice(todos.indexOf(todo), 1);
});
}
function select(todo) {
console.log(todo);
}
</script>
<button on:click={test()}>Test</button>
[ {duration} ]
<ul>
{#each todos as todo }
<li>
<input type="checkbox" bind:checked={todo.done} />
<span on:click={select(todo)} class:inactive={todo.done} style="cursor: pointer;">{todo.name}</span>
<a href on:click|preventDefault={remove(todo)}>[x]</a>
</li>
{/each}
</ul>
<script>
let todos = [];
let duration = '';
function timeit(fn) {
let start = Date.now();
fn();
setTimeout(() => {
duration += ' + ' + (Date.now() - start);
}, 10);
};
function test() {
timeit(() => {
todos.length = 0;
for(let i=0;i<5000;i++) {
todos.push({
name: '' + i,
done: false
});
}
todos=todos;
});
}
function remove(todo) {
timeit(() => {
todos.splice(todos.indexOf(todo), 1);
todos=todos;
});
}
function select(todo) {
console.log(todo);
}
</script>
<style>
.inactive {text-decoration: line-through;}
</style>
<button on:click={() => test()}>Test</button>
[ {duration} ]
<ul>
{#each todos as todo (todo) }
<li>
<input type="checkbox" bind:checked={todo.done} />
<span on:click={() => select(todo)} class:inactive={todo.done} style="cursor: pointer;">{todo.name}</span>
<a href on:click|preventDefault={() => remove(todo)}>[x]</a>
</li>
{/each}
</ul>
<script>
import { ref } from 'vue'
export default {
setup() {
let todos = ref([]);
let duration = ref('');
function timeit(fn) {
let start = Date.now();
fn();
setTimeout(() => {
duration.value += ' + ' + (Date.now() - start);
}, 10);
};
function test() {
timeit(() => {
todos.value.length = 0;
for(let i=0;i<5000;i++) {
todos.value.push({
name: '' + i,
done: false
});
}
});
}
function remove(todo) {
timeit(() => {
todos.value.splice(todos.value.indexOf(todo), 1);
});
}
function select(todo) {
console.log(todo);
}
return {
todos,
duration,
test,
remove,
select
}
}
}
</script>
<template>
<button @click="test">Test</button> [ {{duration}} ]
<ul>
<li v-for="todo in todos" :key="todo">
<input type="checkbox" v-model="todo.done" />
<span @click="select(todo)" class="{inactive: todo.done}" style="cursor: pointer;">{{todo.name}}</span>
<a href @click.prevent="remove(todo)">[x]</a>
</li>
</ul>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment