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

Performance Tweak #1

Closed
adbr0029 opened this issue Jul 19, 2017 · 0 comments
Closed

Performance Tweak #1

adbr0029 opened this issue Jul 19, 2017 · 0 comments

Comments

@adbr0029
Copy link

Needed to generate several millions of VINs and this algorith was not performing so I made a very simple tweak:
extracted calls to _.keys and saved as variable i.e.

var prefixesKeys = _.keys(prefixes);
var vinDigitValuesKeys = _.keys(vinDigitValues)

function generateVin() {
    var prefix = _.sample(prefixesKeys), //here
        code = prefixes[prefix],
        char = _.sample(vinDigitValuesKeys),//here
        chars = _.times(7,_.partial(_.sample, vinDigitValuesKeys)).join(''), //here
        vinPart = prefix + char + code + chars,
        check = getCheckSum(vinPart);

    return vinPart.substring(0,8) + check + vinPart.substring(9,17);
};

Used something like this to test:

var vinGenerator = require(__dirname + '/vin-generator');
console.time("save");
for (i = 0; i < 10000; i++) {
    var randomVin = vinGenerator.generateVin();
}
console.timeEnd("save");

Before change it too 89548.187ms to execute and after in 103.560ms

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

No branches or pull requests

1 participant