Skip to content

Instantly share code, notes, and snippets.

@phpnode
Created December 13, 2016 14:17
Show Gist options
  • Save phpnode/435b5cf963447a275f4726f14301ec99 to your computer and use it in GitHub Desktop.
Save phpnode/435b5cf963447a275f4726f14301ec99 to your computer and use it in GitHub Desktop.
Untitled benchmark (http://jsbench.github.io/#435b5cf963447a275f4726f14301ec99) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Untitled benchmark</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 () {
function getPointer (subject, pointer) {
const {length} = pointer;
let target = subject;
let last = null;
for (let i = 0; i < length; i++) {
if (target == null) {
return;
}
const char = pointer.charCodeAt(i);
if (char === 47) {
if (last === null) {
last = i;
}
else {
const part = pointer.slice(last + 1, i);
last = i;
target = target[part];
}
}
}
if (target != null) {
return target[pointer.slice(last + 1)];
}
else {
return target;
}
}
function getPointer2 (subject, pointer) {
const parts = pointer.split('/');
const {length} = parts;
let target = subject;
for (let i = 0; i < length; i++) {
if (target == null) {
return;
}
const part = parts[i];
if (i === 0 && !part) {
continue;
}
else {
target = target[part];
}
}
return target;
}
};
suite.add("getPointer({a: {b: 1}}, '/a/b');", function () {
getPointer({a: {b: 1}}, '/a/b');
});
suite.add("getPointer2({a: {b: 1}}, '/a/b');", function () {
getPointer2({a: {b: 1}}, '/a/b');
});
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("Untitled benchmark");
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