Skip to content

Commit 77e9fe3

Browse files
committed
chore: early exit when encoding invalid numbers
Before this change, we checked the validity of every number during encoding. Now, we stop if we find an invalid one. In my testing, this was about twice as fast for invalid inputs. It had no performance impact for valid inputs, but should theoretically avoid an unnecessary array allocation.
1 parent ebca95e commit 77e9fe3

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

cjs/sqids.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sqids.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/sqids.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sqids.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,10 @@ export default class Sqids {
628628
return ''
629629
}
630630

631-
const inRangeNumbers = numbers.filter((n) => n >= 0 && n <= this.maxValue())
632-
if (inRangeNumbers.length !== numbers.length) {
631+
const areAllNumbersInRange = numbers.every(
632+
(n) => n >= 0 && n <= this.maxValue(),
633+
)
634+
if (!areAllNumbersInRange) {
633635
throw new Error(
634636
`Encoding supports numbers between 0 and ${this.maxValue()}`,
635637
)

0 commit comments

Comments
 (0)