From fa553aba940060169d05694e4b69b81352fb04ab Mon Sep 17 00:00:00 2001 From: Nathachai Thongniran Date: Thu, 20 Apr 2023 15:49:38 +0700 Subject: [PATCH 1/3] 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()}`) +}) From b1157d358f698f05be94a8be2678540df65c3653 Mon Sep 17 00:00:00 2001 From: Nathachai Thongniran Date: Thu, 20 Apr 2023 15:50:12 +0700 Subject: [PATCH 2/3] fix: add missing packages into package.json --- package.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 1bf549f..fe44eff 100644 --- a/package.json +++ b/package.json @@ -36,10 +36,14 @@ "devDependencies": { "@stryker-mutator/core": "^6.3.1", "@stryker-mutator/jest-runner": "^6.3.1", + "@to-da-moon/thai-baht-lib": "^0.0.12", + "baht": "^0.7.1", "jest": "^26.6.3", "jest-expect-message": "^1.0.2", "semantic-release": "^19.0.5", - "standard": "^15.0.1" + "standard": "^15.0.1", + "thai-baht-text": "^1.0.8", + "thai-baht-text-ts": "^1.1.0" }, "jest": { "setupFilesAfterEnv": [ From a356a6d5c3108efbf815d5e8678597afa8026357 Mon Sep 17 00:00:00 2001 From: Nathachai Thongniran Date: Thu, 20 Apr 2023 15:50:34 +0700 Subject: [PATCH 3/3] perf(bahttext): increase ~18% --- src/index.js | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/src/index.js b/src/index.js index 059151d..a418de5 100644 --- a/src/index.js +++ b/src/index.js @@ -37,13 +37,9 @@ function bahtxtNum2Word (nums) { * @returns {string} */ function bahtxtGrammarFix (str) { - let result = str - - result = result.replace(/หนึ่งสิบ/g, 'สิบ') - result = result.replace(/สองสิบ/g, 'ยี่สิบ') - result = result.replace(/สิบหนึ่ง/g, 'สิบเอ็ด') - - return result + return str.replace(/หนึ่งสิบ/g, 'สิบ') + .replace(/สองสิบ/g, 'ยี่สิบ') + .replace(/สิบหนึ่ง/g, 'สิบเอ็ด') } /** @@ -55,19 +51,15 @@ function bahtxtGrammarFix (str) { * @returns {string} */ function bahtxtCombine (baht, satang) { - let result = '' - - if (baht === '' && satang === '') { - result = bahtxtConst.defaultResult - } else if (baht !== '' && satang === '') { - result = baht + 'บาท' + 'ถ้วน' - } else if (baht === '' && satang !== '') { - result = satang + 'สตางค์' + if (!baht && !satang) { + return bahtxtConst.defaultResult + } else if (baht && !satang) { + return baht + 'บาท' + 'ถ้วน' + } else if (!baht && satang) { + return satang + 'สตางค์' } else { - result = baht + 'บาท' + satang + 'สตางค์' + return baht + 'บาท' + satang + 'สตางค์' } - - return result } /** @@ -77,16 +69,14 @@ function bahtxtCombine (baht, satang) { * @returns {string} */ function bahttext (num) { - // no null - if (!num) return bahtxtConst.defaultResult - // no boolean - if (typeof num === 'boolean') return bahtxtConst.defaultResult - // must be number only - if (isNaN(Number(num))) return bahtxtConst.defaultResult - // not less than Number.MIN_SAFE_INTEGER - if (num < Number.MIN_SAFE_INTEGER) return bahtxtConst.defaultResult - // no more than Number.MAX_SAFE_INTEGER - if (num > Number.MAX_SAFE_INTEGER) return bahtxtConst.defaultResult + if (!num || // no null + typeof num === 'boolean' || // no boolean + isNaN(Number(num)) || // must be number only + num < Number.MIN_SAFE_INTEGER || // not less than Number.MIN_SAFE_INTEGER + num > Number.MAX_SAFE_INTEGER // no more than Number.MAX_SAFE_INTEGER + ) { + return bahtxtConst.defaultResult + } // set const positiveNum = Math.abs(num) @@ -94,7 +84,7 @@ function bahttext (num) { // split baht and satang e.g. 432.214567 >> 432, 21 const bahtStr = Math.floor(positiveNum).toString() /** @type {string} */ - const satangStr = (positiveNum % 1 * 100).toFixed(2).split('.')[0] + const satangStr = (positiveNum % 1 * 100).toFixed(0) /** @type {number[]} */ const bahtArr = Array.from(bahtStr).map(Number)