Skip to content

Commit

Permalink
improvement: Perf boost with custom alphabet
Browse files Browse the repository at this point in the history
Improve perf by ensuring that the `alphabetIndexMap` array is not marked as "holey".
  • Loading branch information
nwoltman committed Oct 29, 2019
1 parent 65d397b commit 21e061a
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions natural-compare.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

var alphabet;
var alphabetIndexMap;
var alphabetIndexMapLength = 0;
var alphabetIndexMap = [];

function isNumberCode(code) {
return code >= 48 && code <= 57;
Expand Down Expand Up @@ -74,8 +73,8 @@ function naturalCompare(a, b, opts) {

if (charCodeA !== charCodeB) {
if (
charCodeA < alphabetIndexMapLength &&
charCodeB < alphabetIndexMapLength &&
charCodeA < alphabetIndexMap.length &&
charCodeB < alphabetIndexMap.length &&
alphabetIndexMap[charCodeA] !== -1 &&
alphabetIndexMap[charCodeB] !== -1
) {
Expand Down Expand Up @@ -110,20 +109,20 @@ Object.defineProperties(naturalCompare, {
alphabet = value;
alphabetIndexMap = [];

var i = 0;

if (alphabet) {
for (; i < alphabet.length; i++) {
alphabetIndexMap[alphabet.charCodeAt(i)] = i;
}
if (!alphabet) {
return;
}

alphabetIndexMapLength = alphabetIndexMap.length;
const maxCharCode = alphabet.split('').reduce((maxCode, char) => {
return Math.max(maxCode, char.charCodeAt(0));
}, 0);

for (i = 0; i < alphabetIndexMapLength; i++) {
if (alphabetIndexMap[i] === undefined) {
alphabetIndexMap[i] = -1;
}
for (let i = 0; i <= maxCharCode; i++) {
alphabetIndexMap.push(-1);
}

for (let i = 0; i < alphabet.length; i++) {
alphabetIndexMap[alphabet.charCodeAt(i)] = i;
}
},
},
Expand Down

0 comments on commit 21e061a

Please sign in to comment.