Skip to content

Instantly share code, notes, and snippets.

@benmccann
Created June 8, 2020 03:40
Show Gist options
  • Save benmccann/9fce8cede5ed1fe15e7d1b99ab3890cd to your computer and use it in GitHub Desktop.
Save benmccann/9fce8cede5ed1fe15e7d1b99ab3890cd to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>svelte-create</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
// SVELTE INTERNALS
function noop() { }
function append(target, node) {
target.appendChild(node);
}
function insert(target, node, anchor) {
target.insertBefore(node, anchor || null);
}
function detach(node) {
node.parentNode.removeChild(node);
}
function element(name) {
return document.createElement(name);
}
function text(data) {
return document.createTextNode(data);
}
function space() {
return text(' ');
}
function set_data(text, data) {
data = '' + data;
if(text.data !== data)
text.data = data;
}
// END SVELTE INTERNALS
function create_fragment_imperative(ctx) {
let section;
let h1;
let t3;
let p;
return {
c() {
section = element("section");
h1 = element("h1");
h1.textContent = `Hello ${ctx.name}!`;
t3 = space();
p = element("p");
p.textContent = `${ctx.description}`;
},
m(target, anchor) {
insert(target, section, anchor);
append(section, h1);
append(section, t3);
append(section, p);
},
p: noop,
i: noop,
o: noop,
d(detaching) {
if(detaching) detach(section);
}
};
}
function make_renderer(html) {
const template = document.createElement('template');
template.innerHTML = html;
const text = template.content.querySelectorAll('sveltetext');
for(let i = 0; i < text.length; i += 1) {
text[ i ].replaceWith(document.createTextNode(""));
}
return () => {
const fragment = template.content.cloneNode(true);
return fragment.querySelectorAll('[bind]');
};
}
const render = make_renderer(
`<section bind><h1 bind>Hello <sveltetext/>!</h1> <p bind><sveltetext/></p></section>`
);
function create_fragment_template(ctx) {
let section;
let t0;
let t1;
return {
c() {
[ section, { childNodes: [ , t0 ] }, { childNodes: [ t1 ] } ] = render();
t0 = set_data(t0, ctx.name);
t1 = set_data(t1, ctx.description);
},
m(target, anchor) {
insert(target, section, anchor);
},
i: noop,
o: noop,
d(detaching) {
if(detaching) detach(section);
}
};
}
};
suite.add("let name = \"world\";", function () {
let name = "world";
let description = "gogogo";
const frag = create_fragment_imperative({ name, description });
frag.c();
frag.m(document.body);
});
suite.add("let name = \"world\";", function () {
let name = "world";
let description = "gogogo";
const frag = create_fragment_template({ name, description });
frag.c();
frag.m(document.body);
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("svelte-create");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment