Created
June 10, 2021 19:54
-
-
Save khaosdoctor/1cdaca13a9ecc3bd13321bf29c71d55e to your computer and use it in GitHub Desktop.
Benchmark for transaction balance (https://jsbench.github.io/#1cdaca13a9ecc3bd13321bf29c71d55e) #jsbench #jsperf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Benchmark for transaction balance</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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
suite.add("const transactions = [", function () { | |
const transactions = [ | |
{ | |
id: "11ff73b5-e771-441c-886a-498d93b5093d", | |
sourceAccount: "my_account", | |
targetAccount: "book_store", | |
amount: -9600, | |
currency: "EUR", | |
category: "entertainment", | |
time: "2021-04-08T05:15:56.905Z", | |
}, | |
{ | |
id: "8c3ec38d-1821-4d49-aef1-2385cb3c2b1b", | |
sourceAccount: "my_account", | |
targetAccount: "cinema", | |
amount: -5700, | |
currency: "EUR", | |
category: "entertainment", | |
time: "2021-04-07T21:16:57.819Z", | |
}, | |
{ | |
id: "d1c77d7c-ccda-453c-ac01-444e9d5abca3", | |
sourceAccount: "my_account", | |
targetAccount: "book_store", | |
amount: -7400, | |
currency: "EUR", | |
category: "entertainment", | |
time: "2021-04-07T22:46:44.071Z", | |
}, | |
{ | |
id: "837127ab-f523-4b11-bed3-ae488be4545d", | |
sourceAccount: "my_account", | |
targetAccount: "fitness_club", | |
amount: -9200, | |
currency: "EUR", | |
category: "sports", | |
time: "2021-04-05T01:55:16.646Z", | |
}, | |
] | |
const categories = ['sports', 'entertainment'] | |
const start = new Date('2021-04-07') | |
const end = new Date('2021-04-08') | |
const getBalanceByCategoryInPeriod = ( | |
transactions, | |
categories, | |
start, | |
end | |
) => { | |
return transactions.reduce((acc, transaction) => { | |
if (!categories.includes(transaction.category)) return acc | |
const transactionTime = new Date(transaction.time) | |
if (transactionTime < start || transactionTime >= end) return acc | |
acc[transaction.category] = (acc[transaction.category] || 0) + transaction.amount | |
return acc | |
}, {}) | |
}; | |
getBalanceByCategoryInPeriod(transactions, categories, start, end) | |
}); | |
suite.add("const transactions = [", function () { | |
const transactions = [ | |
{ | |
id: "11ff73b5-e771-441c-886a-498d93b5093d", | |
sourceAccount: "my_account", | |
targetAccount: "book_store", | |
amount: -9600, | |
currency: "EUR", | |
category: "entertainment", | |
time: "2021-04-08T05:15:56.905Z", | |
}, | |
{ | |
id: "8c3ec38d-1821-4d49-aef1-2385cb3c2b1b", | |
sourceAccount: "my_account", | |
targetAccount: "cinema", | |
amount: -5700, | |
currency: "EUR", | |
category: "entertainment", | |
time: "2021-04-07T21:16:57.819Z", | |
}, | |
{ | |
id: "d1c77d7c-ccda-453c-ac01-444e9d5abca3", | |
sourceAccount: "my_account", | |
targetAccount: "book_store", | |
amount: -7400, | |
currency: "EUR", | |
category: "entertainment", | |
time: "2021-04-07T22:46:44.071Z", | |
}, | |
{ | |
id: "837127ab-f523-4b11-bed3-ae488be4545d", | |
sourceAccount: "my_account", | |
targetAccount: "fitness_club", | |
amount: -9200, | |
currency: "EUR", | |
category: "sports", | |
time: "2021-04-05T01:55:16.646Z", | |
}, | |
] | |
const categories = ['sports', 'entertainment'] | |
const start = new Date('2021-04-07') | |
const end = new Date('2021-04-08') | |
const getBalanceByCategoryInPeriod = ( | |
transactions, | |
categories, | |
start, | |
end | |
) => { | |
const balance = {} | |
for (const transaction of transactions) { | |
if (!categories.includes(transaction.category)) continue | |
const transactionTime = new Date(transaction.time) | |
if (transactionTime < start || transactionTime >= end) continue | |
balance[transaction.category] = (balance[transaction.category] || 0) + transaction.amount | |
} | |
return balance | |
}; | |
getBalanceByCategoryInPeriod(transactions, categories, start, end) | |
}); | |
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("Benchmark for transaction balance"); | |
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