Skip to content

Commit

Permalink
Adding benchmarks, fixes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxArt2501 committed Aug 18, 2015
1 parent 455c49c commit 032da54
Show file tree
Hide file tree
Showing 9 changed files with 541 additions and 23 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Massimo Artizzu (MaxArt2501)
Copyright (c) 2014-2015 Massimo Artizzu (MaxArt2501)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
143 changes: 143 additions & 0 deletions benchmark/benchmarks-lite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
(function(root, tests) {
if (typeof define === "function" && define.amd)
define(["expect", "object-observe-lite"], tests);
else if (typeof exports === "object")
tests(require("benchmark"), global, require("../dist/object-observe-lite.js"));
else tests(root.Benchmark, root);
})(this, function(Benchmark, root) {
"use strict";

root.generateObject = function(numberOfProperties) {
var object = {};

for (var i = 0; i < numberOfProperties; i++)
object["prop" + i] = i;

return object;
}

function noop() {}

var padspace = (new Array(81)).join(" ");
function padL(text, length) {
return (padspace + text).slice(-length);
}
function padR(text, length) {
return (text + padspace).slice(0, length);
}

var onDOM = typeof document !== "undefined";

if (onDOM) {
var parent = document.getElementsByTagName("tbody")[0];

var initBench = function() {
var row = this.row = document.createElement("tr"),
cell = document.createElement("td");
cell.innerHTML = this.props;
row.appendChild(cell);
cell = document.createElement("td");
cell.innerHTML = this.objects;
row.appendChild(cell);
cell = document.createElement("td");
cell.className = "changes";
cell.innerHTML = this.name;
row.appendChild(cell);

this.cells = [];
cell = document.createElement("td");
cell.className = "samples";
this.cells.push(cell);
row.appendChild(cell);
cell = document.createElement("td");
cell.className = "count";
this.cells.push(cell);
row.appendChild(cell);
cell = document.createElement("td");
cell.className = "frequency";
this.cells.push(cell);
row.appendChild(cell);

parent.appendChild(row);
},
onCycle = function() {
this.cells[0].innerHTML = ++this.samples;
this.cells[1].innerHTML = this.count;
this.cells[2].innerHTML = this.hz.toFixed(2);
};
} else {
var writeOut = typeof process === "undefined" || !process.stdout || !process.stdout.isTTY
? function(text) { console.log(text); }
: function(text) { process.stdout.write(text); };
writeOut("\x1b[1;37mProperties Objects Changes Samples Loops FPS (Hz)\n");
writeOut( "---------------------------------------------------------------------");
var initBench = function() {
writeOut("\n\x1b[1;30m" + padL(this.props, 10) + " " + padL(this.objects, 7) + " " + padR(this.name, 48));
},
onCycle = function() {
writeOut("\x1b[38D\x1b[1;31m" + padL(++this.samples, 9) + "\x1b[1;36m" + padL(this.count, 12)
+ "\x1b[1;32m" + padL(this.hz.toFixed(2), 17) + "\x1b[0;37m");
};
}

function errorBench(e) {
console.log(e);
}

var benches = [];

function generateBenchGroup(props, objects) {
var propsPerObject = props / objects;
var options = {
setup: "\
var handler = function() {};\n\
var objects = [];\n\
for (var i = 0; i < " + objects + "; i++) {\n\
objects[i] = generateObject(" + propsPerObject + ");\n\
Object.observe(objects[i], handler);\n\
}\n",
teardown: "\
for (var i = 0; i < " + objects + "; i++)\
Object.unobserve(objects[i], handler);\n",
props: props,
objects: objects,
onStart: initBench,
onCycle: onCycle,
onError: errorBench,
onComplete: nextBench,
async: true,
samples: 0
};
var midIndex = objects >> 1,
midProp = "prop" + (propsPerObject >> 1);

benches.push(new Benchmark("none", "\
Object.deliverChangeRecords(handler);\n", options));
benches.push(new Benchmark("one", "\
objects[" + midIndex + "]." + midProp + "++;\n\
Object.deliverChangeRecords(handler);\n", options));
benches.push(new Benchmark("all", "\
for (var i = 0; i < " + objects + "; i++)\n\
for (var j = 0; j < " + propsPerObject + "; j++)\n\
objects[i][\"prop\" + j]++;\n\
Object.deliverChangeRecords(handler);\n", options));
}

generateBenchGroup(10, 1);
generateBenchGroup(10, 2);
generateBenchGroup(100, 1);
generateBenchGroup(100, 20);
generateBenchGroup(1000, 1);
generateBenchGroup(1000, 200);

var index = 0;

function nextBench() {
if (index >= benches.length) return;

benches[index++].run();
}

nextBench();

});
143 changes: 143 additions & 0 deletions benchmark/benchmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
(function(root, tests) {
if (typeof define === "function" && define.amd)
define(["expect", "object-observe"], tests);
else if (typeof exports === "object")
tests(require("benchmark"), global, require("../dist/object-observe.js"));
else tests(root.Benchmark, root);
})(this, function(Benchmark, root) {
"use strict";

root.generateObject = function(numberOfProperties) {
var object = {};

for (var i = 0; i < numberOfProperties; i++)
object["prop" + i] = i;

return object;
}

function noop() {}

var padspace = (new Array(81)).join(" ");
function padL(text, length) {
return (padspace + text).slice(-length);
}
function padR(text, length) {
return (text + padspace).slice(0, length);
}

var onDOM = typeof document !== "undefined";

if (onDOM) {
var parent = document.getElementsByTagName("tbody")[0];

var initBench = function() {
var row = this.row = document.createElement("tr"),
cell = document.createElement("td");
cell.innerHTML = this.props;
row.appendChild(cell);
cell = document.createElement("td");
cell.innerHTML = this.objects;
row.appendChild(cell);
cell = document.createElement("td");
cell.className = "changes";
cell.innerHTML = this.name;
row.appendChild(cell);

this.cells = [];
cell = document.createElement("td");
cell.className = "samples";
this.cells.push(cell);
row.appendChild(cell);
cell = document.createElement("td");
cell.className = "count";
this.cells.push(cell);
row.appendChild(cell);
cell = document.createElement("td");
cell.className = "frequency";
this.cells.push(cell);
row.appendChild(cell);

parent.appendChild(row);
},
onCycle = function() {
this.cells[0].innerHTML = ++this.samples;
this.cells[1].innerHTML = this.count;
this.cells[2].innerHTML = this.hz.toFixed(2);
};
} else {
var writeOut = typeof process === "undefined" || !process.stdout || !process.stdout.isTTY
? function(text) { console.log(text); }
: function(text) { process.stdout.write(text); };
writeOut("\x1b[1;37mProperties Objects Changes Samples Loops FPS (Hz)\n");
writeOut( "---------------------------------------------------------------------");
var initBench = function() {
writeOut("\n\x1b[1;30m" + padL(this.props, 10) + " " + padL(this.objects, 7) + " " + padR(this.name, 48));
},
onCycle = function() {
writeOut("\x1b[38D\x1b[1;31m" + padL(++this.samples, 9) + "\x1b[1;36m" + padL(this.count, 12)
+ "\x1b[1;32m" + padL(this.hz.toFixed(2), 17) + "\x1b[0;37m");
};
}

function errorBench(e) {
console.log(e);
}

var benches = [];

function generateBenchGroup(props, objects) {
var propsPerObject = props / objects;
var options = {
setup: "\
var handler = function() {};\n\
var objects = [];\n\
for (var i = 0; i < " + objects + "; i++) {\n\
objects[i] = generateObject(" + propsPerObject + ");\n\
Object.observe(objects[i], handler);\n\
}\n",
teardown: "\
for (var i = 0; i < " + objects + "; i++)\
Object.unobserve(objects[i], handler);\n",
props: props,
objects: objects,
onStart: initBench,
onCycle: onCycle,
onError: errorBench,
onComplete: nextBench,
async: true,
samples: 0
};
var midIndex = objects >> 1,
midProp = "prop" + (propsPerObject >> 1);

benches.push(new Benchmark("none", "\
Object.deliverChangeRecords(handler);\n", options));
benches.push(new Benchmark("one", "\
objects[" + midIndex + "]." + midProp + "++;\n\
Object.deliverChangeRecords(handler);\n", options));
benches.push(new Benchmark("all", "\
for (var i = 0; i < " + objects + "; i++)\n\
for (var j = 0; j < " + propsPerObject + "; j++)\n\
objects[i][\"prop\" + j]++;\n\
Object.deliverChangeRecords(handler);\n", options));
}

generateBenchGroup(10, 1);
generateBenchGroup(10, 2);
generateBenchGroup(100, 1);
generateBenchGroup(100, 20);
generateBenchGroup(1000, 1);
generateBenchGroup(1000, 200);

var index = 0;

function nextBench() {
if (index >= benches.length) return;

benches[index++].run();
}

nextBench();

});
48 changes: 48 additions & 0 deletions benchmark/index-lite.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Object.observe polyfill tests</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
table {
border-collapse: collapse;
}
th {
text-align: left;
padding: 4px 10px;
border-bottom: 1px solid lightgray;
}
td {
padding: 4px 10px;
text-align: right;
background-color: #aff;
}
.changes { text-align: left; }
.samples { background-color: #fdd; }
.count { background-color: #ddf; }
.frequency { background-color: lime; }
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Properties</th>
<th>Objects</th>
<th>Changes</th>
<th>Samples</th>
<th>Loops</th>
<th>FPS (Hz)</th>
</tr>
</thead>
<tbody></tbody>
</table>
<a href="index.html">Regular version</a>
<script src="../node_modules/benchmark/benchmark.js"></script>
<script src="../dist/object-observe-lite.js"></script>
<script src="benchmarks-lite.js"></script>
</body>
</html>
48 changes: 48 additions & 0 deletions benchmark/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Object.observe polyfill tests</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
table {
border-collapse: collapse;
}
th {
text-align: left;
padding: 4px 10px;
border-bottom: 1px solid lightgray;
}
td {
padding: 4px 10px;
text-align: right;
background-color: #aff;
}
.changes { text-align: left; }
.samples { background-color: #fdd; }
.count { background-color: #ddf; }
.frequency { background-color: lime; }
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Properties</th>
<th>Objects</th>
<th>Changes</th>
<th>Samples</th>
<th>Loops</th>
<th>FPS (Hz)</th>
</tr>
</thead>
<tbody></tbody>
</table>
<a href="index-lite.html">Lite version</a>
<script src="../node_modules/benchmark/benchmark.js"></script>
<script src="../dist/object-observe.js"></script>
<script src="benchmarks.js"></script>
</body>
</html>
Loading

0 comments on commit 032da54

Please sign in to comment.