Skip to content

Instantly share code, notes, and snippets.

@mofux
Last active July 4, 2017 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mofux/9f772c6df61ca6dea50e39298785d99b to your computer and use it in GitHub Desktop.
Save mofux/9f772c6df61ca6dea50e39298785d99b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>CharData vs []</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 () {
class CharData extends Array {
constructor(char, width, flags, fg, bg) {
super(char, width, flags, fg, bg)
}
get char() {
return this[0]
}
get width() {
return this[1]
}
get flags() {
return this[2]
}
get fg() {
return this[3]
}
get bg() {
return this[4]
}
}
let toCharData = (arr) => ({ char: arr[0], width: arr[1], flags: arr[2], fg: arr[3], bg: arr[4] });
let arrdata = ['a', 1, 0, 1 << 24, 1 << 24];
let chdata = new CharData('a', 1, 0, 1 << 24, 1 << 24);
};
suite.add("create: char-data", function () {
// create: char-data
let arr = new CharData('a', 1, 0, 1 << 24, 1 << 24);
});
suite.add("create: native array", function () {
// create: native array
let arr = ['a', 1, 0, 1 << 24, 1 << 24];
});
suite.add("read: char-data", function () {
// read: char-data
let str = `${chdata.char},${chdata.width},${chdata.flags},${chdata.fg},${chdata.bg}`;
});
suite.add("read: native array", function () {
// read: native array
let str = `${arrdata[0]},${arrdata[1]},${arrdata[2]},${arrdata[3]},${arrdata[4]}`
});
suite.add("read: array through view", function () {
// read: array through view
let view = toCharData(arrdata);
let str = `${view.char},${view.width},${view.flags},${view.fg},${view.bg}`;
});
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("CharData vs []");
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