Optimize performance

pull/3/head
Amio 2018-06-29 18:25:00 +08:00
rodzic a8cb2f32af
commit b7c0ad38c0
1 zmienionych plików z 9 dodań i 3 usunięć

Wyświetl plik

@ -10,9 +10,15 @@ function calcWidth (charWidthTable) {
if (typeof text !== 'string') {
return 0
} else {
return Array.from(text).reduce((total, curr) => {
return total + (widthTable[curr.charCodeAt()] || widthTable[64]) // "@"
}, 0)
let total = 0
let i = text.length
let charCode = 0
while (i--) {
charCode = text[i].charCodeAt()
total += widthTable[charCode < 127 ? charCode : 64] // "@" for overflows
}
console.log(total)
return total
}
}
}