Skip to content

Instantly share code, notes, and snippets.

@jamie-pate
Last active September 20, 2017 22:52
Show Gist options
  • Save jamie-pate/2a63e16d463941667c36e75aa581d343 to your computer and use it in GitHub Desktop.
Save jamie-pate/2a63e16d463941667c36e75aa581d343 to your computer and use it in GitHub Desktop.
Array_splice_or_hand_copy #jsbench #jsperf (http://jsbench.github.io/#2a63e16d463941667c36e75aa581d343) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Array_splice_or_hand_copy #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 () {
Array.from = Array.from || function(a) {
var result = new Array(a.length), len = a.length;
for (i = 0; i < len; ++i) {
result[i] = a[i] === undefined ? null : a[i];
}
return result;
}
var
N = 10000;
bigArray = JSON.stringify(Array.from(new Array(N)).map(function(e, i) { return {oi: i, i:i} })),
index = Math.round(N/20),
after = Math.round(N/2);
function verify(arr) {
for (i = 0; i < arr.length; ++i) {
if (arr[i].i !== i) {
console.error(i, arr[i]);
console.error('i doesnt match ' + i + ' != ' + arr[i].i);
debugger;
throw new Error('i doesnt match ' + i + ' != ' + arr[i].i);
}
var expectedIndex = i;
if (i === after) {
expectedIndex = index;
} else if (i >= index && i < after) {
expectedIndex = i + 1;
}
if (arr[i].oi !== expectedIndex) {
console.error(arr[i]);
console.log('oi doesnt match ' + i + ' != ' + arr[i].oi);
debugger;
throw new Error('oi doesnt match ' + i + ' != ' + arr[i].oi);
}
}
}
function copy(item) { return Object.assign({}, item); }
};
Benchmark.prototype.teardown = function () {
verify(newArray);
};
suite.add("copy by hand", function () {
// copy by hand
var newArray = JSON.parse(bigArray),
i,
row = newArray[index];
for (i = index + 1; i <= after; ++i) {
--newArray[i].i;
newArray[i - 1] = newArray[i];
}
row.i = after;
newArray[after] = row;
});
suite.add("copy by slice", function () {
// copy by slice
var newArray = JSON.parse(bigArray),
middle = newArray.slice(index + 1, after + 1),
row = newArray[index],
end = newArray.slice(after + 1);
middle.forEach(function(m) { --m.i });
row.i = after;
newArray = newArray.slice(0, index).concat(middle).concat([row]).concat(end);
});
suite.add("copy by splice", function () {
// copy by splice
var newArray = JSON.parse(bigArray),
row = newArray[index],
middle = newArray.slice(index + 1, after + 1);
middle.forEach(function(m) { --m.i });
row.i = after;
newArray.splice.apply(newArray, [index, after - index + 1].concat(middle).concat([row]));
});
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("Array_splice_or_hand_copy #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