Set.prototype.has VS RegExp.prototype.test VS Array.prototype.includes

by kimizuka

JavaScript

const exponent = 6;
const length = Math.pow(10, exponent);
const array = Array.from({ length }, (_, i) => `user-${String(i).padStart(exponent, '0')}`);

const set = new Set(array);

console.time('Set.prototype.has');
console.log(set.has(`user-${ String(length -1).padStart(exponent, '0') }`));
console.timeEnd('Set.prototype.has');

const str = array.join(',');
const regExp = new RegExp(`\\buser-${ String(length -1).padStart(exponent, '0') }\\b`);

console.time('RegExp.prototype.test');
console.log(regExp.test(str));
console.timeEnd('RegExp.prototype.test');

console.time('Array.prototype.includes');
console.log(array.includes(`user-${ String(length -1).padStart(exponent, '0') }`));
console.timeEnd('Array.prototype.includes');