Skip to content

Instantly share code, notes, and snippets.

@cvializ
Last active August 10, 2018 14:07
Show Gist options
  • Save cvializ/e104ebb4a49cb1b1c19237c9add27d1c to your computer and use it in GitHub Desktop.
Save cvializ/e104ebb4a49cb1b1c19237c9add27d1c to your computer and use it in GitHub Desktop.
Iterating with Dates #jsbench #jsperf (http://jsbench.github.io/#e104ebb4a49cb1b1c19237c9add27d1c) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Iterating with Dates #jsbench #jsperf</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 () {
const container = document.createElement('div');
container.classList.add('cv-test-container');
for (let i = 0; i < 1000; i++) {
container.appendChild(document.createElement('div'));
}
document.body.appendChild(container);
function getFirstDayOfMonth(date) { return new Date(date.getFullYear(), date.getMonth(), 1); }
const date = getFirstDayOfMonth(new Date());
const children = container.children;
};
Benchmark.prototype.teardown = function () {
document.querySelector('.cv-test-container').remove();
};
suite.add("Copy via Date functions", function () {
// Copy via Date functions
const year = date.getFullYear();
const month = date.getMonth();
for (let i = 1; i < 31; i++) {
children[i].innerText = new Date(year, month, i).toLocaleString('fa', {day: 'numeric'});
}
});
suite.add("Create via millis and addition", function () {
// Create via millis and addition
let d = +date;
for (let i = 1; i < 31; i++) {
children[i].innerText = new Date(d).toLocaleString('fa', {day: 'numeric'});
d += 86400000
}
});
suite.add("Create via millis and multiplication", function () {
// Create via millis and multiplication
const d = +date;
for (let i = 0; i < 31; i++) {
children[i].innerText = new Date(d + i * 8640000).toLocaleString('fa', {day: 'numeric'});
}
});
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("Iterating with Dates #jsbench #jsperf");
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