Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GauthamBanasandra/a01e80a7a70e969bb9dc87f0a68b1567 to your computer and use it in GitHub Desktop.
Save GauthamBanasandra/a01e80a7a70e969bb9dc87f0a68b1567 to your computer and use it in GitHub Desktop.
Compares the slow down caused due to transpilation.
/**
* Created by gautham on 04/05/17.
*/
let process = require('process');
function run(size) {
function generate_data(size) {
let data = [];
for (let i = 0; i < size; ++i) {
data.push(i);
}
return data;
}
function N1qlQuery(size) {
this.isInstance = true;
this.data = generate_data(size);
this.stopSignal = false;
this.metaData = {};
this.iter = function (callback) {
for (let item in this.data) {
if (this.stopSignal) {
this.stopSignal = false;
return this.metaData;
}
callback(item);
}
};
this.stopIter = function (stopData) {
this.stopSignal = true;
this.metaData = stopData;
}
}
let res1 = generate_data(size),
sum = 0,
normalIterations = 0,
transpiledIterations = 0;
let start = process.hrtime()[1];
// Normal code.
for (let item1 of res1) {
sum += item1;
++normalIterations;
}
let end = process.hrtime()[1];
let normalCodeTime = end - start;
res1 = new N1qlQuery(size);
res2 = new N1qlQuery(size);
res3 = new N1qlQuery(size);
transpiledIterations = sum = 0;
start = process.hrtime()[1];
// Transpiled code.
if (res1.isInstance) {
res1.x = res1.iter(function (item1) {
sum += item1;
++transpiledIterations;
});
} else {
for (let item1 of res1) {
sum += item1;
++transpiledIterations;
}
}
end = process.hrtime()[1];
let transpiledCodeTime = end - start;
if (normalIterations !== transpiledIterations)
throw 'iterations are not equal';
if (transpiledCodeTime < 0 || normalCodeTime < 0)
return 0;
return transpiledCodeTime / normalCodeTime;
}
function mean(data) {
let sum = 0;
for (let number of data) {
sum += number;
}
return sum / data.length;
}
function median(data) {
data = JSON.parse(JSON.stringify(data));
data.sort(function (a, b) {
return a - b;
});
let mid = Math.floor(data.length / 2);
return data.length % 2 ? data[mid] : (data[mid - 1] + data[mid]) / 2;
}
const SIZE = 100000,
RUNS = 20;
let runs = [];
for (let i = 0; i < RUNS; ++i) {
runs.push(run(SIZE));
}
// runs.sort(function (a, b) {
// return a - b;
// });
console.log(runs);
console.log('mean =', mean(runs));
console.log('median =', median(runs));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment