Skip to content

Commit

Permalink
perf(benchmark): add benchmark.js to validate the performance against…
Browse files Browse the repository at this point in the history
… other libs
  • Loading branch information
jojoee committed Apr 20, 2023
1 parent b198041 commit fa553ab
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -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()}`)
})

0 comments on commit fa553ab

Please sign in to comment.