From fa553aba940060169d05694e4b69b81352fb04ab Mon Sep 17 00:00:00 2001 From: Nathachai Thongniran Date: Thu, 20 Apr 2023 15:49:38 +0700 Subject: [PATCH] perf(benchmark): add benchmark.js to validate the performance against other libs --- benchmark.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 benchmark.js diff --git a/benchmark.js b/benchmark.js new file mode 100644 index 0000000..8bcdcb7 --- /dev/null +++ b/benchmark.js @@ -0,0 +1,29 @@ +const { bahttext } = require('./src') +const THBText = require('thai-baht-text') +const { ThaiBaht } = require('thai-baht-text-ts') +const { convert } = require('baht') + +const allTestcases = require('./misc/testcases.json') +const nTestCases = allTestcases.length +const testcases = allTestcases.slice(0, nTestCases) +const numbers = testcases.map(item => parseFloat(item.number)) +const nIterations = 10000 + +const testedLib = { + bahttext: n => bahttext(n), + baht: n => convert(n), + 'thai-baht-text': n => THBText(n), + 'thai-baht-text-ts': n => ThaiBaht(n) +} + +Object.entries(testedLib).forEach(([name, fn]) => { + const start = new Date() + + new Array(nIterations).fill(0).forEach(_ => { + numbers.forEach(number => fn(number)) + }) + + const end = new Date() + const elapsed = end - start + console.log(`${name}: elapsed: ${elapsed} ms, start: ${start.toISOString()}, end: ${end.toISOString()}`) +})