=== in constant time for strings
npm install constant-equals===.
a === b will take more time to execute if they share a bigger prefix. So checking the user input against a target password with === will leak how much the attacker got the password right:
npm install constant-equals --save
javascript
var a = 'a-user-input',
g = 'target-password',
eq = require('constant-equals')
if (eq(a, b)) {
console.log('Welcome')
} else {
console.log('Go away!')
}
`
eq() doesn't do any kind of type conversion, so eq('12', 12) === false.
Arrays
eq() also works for a pair of arrays:
`javascript
eq(['a', 'array', 'of', 5, 'tags'], ['a', 'array', 'of', 5, 'tags']) === true
`
$3
Like native indexOf() and lastIndexOf() for arrays:
`js
eq.indexOf(['ab', 'cd', 'cd'], 'cd') === 1
eq.indexOf(['ab', 'cd', 'cd'], 'x') === -1
eq.lastIndexOf(['ab', 'cd', 'cd'], 'cd') === 2
``