Skip to content

Instantly share code, notes, and snippets.

@KillyMXI
Created October 8, 2020 11:04
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 KillyMXI/87799f981586ca50fa03a337a905f779 to your computer and use it in GitHub Desktop.
Save KillyMXI/87799f981586ca50fa03a337a905f779 to your computer and use it in GitHub Desktop.
Matrix (2D-array) transpose (http://jsbench.github.io/#87799f981586ca50fa03a337a905f779) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Matrix (2D-array) transpose</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 () {
var matrix = [
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99],
];
function transpose_map_1 (matrix) {
return matrix[0].map((col, i) => matrix.map(row => row[i]));
}
function transpose_map_2 (matrix) {
return matrix[0].map((col, c) => matrix.map((row, r) => matrix[r][c]));
}
function transpose_reduce (matrix) {
return matrix.reduce((prev, next) => next.map((item, i) =>
(prev[i] || []).concat(next[i])
), []);
}
function transpose_vanilla (matrix) {
const rows = matrix.length, cols = matrix[0].length;
const grid = [];
for (let j = 0; j < cols; j++) {
grid[j] = Array(rows);
}
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
grid[j][i] = matrix[i][j];
}
}
return grid;
}
function transpose_inplace_1 (matrix) {
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < i; j++) {
const temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
function transpose_inplace_2 (matrix) {
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < i; j++) {
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
}
}
}
};
suite.add("map 1", function () {
// map 1
var transposed = transpose_map_1(matrix);
});
suite.add("map 2", function () {
// map 2
var transposed = transpose_map_2(matrix);
});
suite.add("reduce", function () {
// reduce
var transposed = transpose_reduce(matrix);
});
suite.add("vanilla", function () {
// vanilla
var transposed = transpose_vanilla(matrix);
});
suite.add("inplace 1", function () {
// inplace 1
var transposed = transpose_inplace_1(matrix);
});
suite.add("inplace 2", function () {
// inplace 2
var transposed = transpose_inplace_2(matrix);
});
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("Matrix (2D-array) transpose");
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