Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimize code even further #40

Merged
merged 1 commit into from
Oct 20, 2023
Merged

Conversation

LinusU
Copy link
Contributor

@LinusU LinusU commented Oct 20, 2023

This brings a speed up in all of the test cases I have tried:

String x times faster
(empty string) 1.00 ± 0.02
5512345678 2.66 ± 0.04
0000000000000000 4.68 ± 0.05
4242424242424241 1.64 ± 0.13
4242424242424242 1.62 ± 0.02
551234- asd- asd- sa -d as9d0 sad9sd90sa 5678 28.79 ± 3.28

@LinusU
Copy link
Contributor Author

LinusU commented Oct 20, 2023

I measured this by first creating index-next.js where I made my changes, I then added two files a.js and b.js which looked like this:

const luhn = require('./index.js') // or index-next

for (let i = 0; i < 1_000_000; i++) luhn('5512345678')

And finally used hyperfine to measure:

$ hyperfine 'node a.js' 'node b.js'
Benchmark 1: node a.js
  Time (mean ± σ):     134.6 ms ±   1.3 ms    [User: 131.1 ms, System: 4.3 ms]
  Range (min … max):   133.5 ms … 139.6 ms    21 runs
 
Benchmark 2: node b.js
  Time (mean ± σ):      50.9 ms ±   0.6 ms    [User: 47.5 ms, System: 3.9 ms]
  Range (min … max):    50.1 ms …  53.8 ms    55 runs
 
Summary
  node b.js ran
    2.65 ± 0.04 times faster than node a.js


return sum % 10 === 0
let index = number.length
let x2 = true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing bit to a boolean led to a speed up, and then I thought that x2 was a better name, since it determines wether to multiply the number by 2 or not


while (index) {
const value = number.charCodeAt(--index) - 48
if (value < 0 || value > 9) return false
Copy link
Contributor Author

@LinusU LinusU Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation had sum turn into NaN which resulted in false being returned in the end, so this should mimic that behaviour

let bit = 1
let sum = 0
let value
const lookup = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed this lookup from array since it's a lookup table for (x * 2) % 10 and array felt very generic

Copy link
Owner

@bendrucker bendrucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@bendrucker bendrucker merged commit 79a5f61 into bendrucker:master Oct 20, 2023
1 check passed
@LinusU LinusU deleted the speed branch October 22, 2023 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants