jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
const sanitizeSearchTerms = (query) => {
return query.replace(/[^a-zA-Z0-9 ]/g, '');
}
const ALL_TERMS = (query) => {
const terms = sanitizeSearchTerms(query).split(' ');
return new RegExp(`(?=.*${terms.join(')(?=.*')})`, 'i');
}
const generateSampleData = (size) => {
const data = [];
for (let i = 0; i < size; i++) {
data.push(`${Math.random().toString(36).substring(2, 15)}${Math.random().toString(36).substring(2, 15)}`);
}
return data;
};
// Approach using Regular Expression
const benchmarkRegex = (items, query) => {
const start = performance.now();
const filteredItems = items.filter((i) => i.match(ALL_TERMS(query)));
const end = performance.now();
console.log(`Regular Expression: ${end - start} milliseconds`);
};
// Approach using indexOf
const benchmarkIndexOf = (items, query) => {
const start = performance.now();
const queryWords = query.toLowerCase().split(' ');
const filteredItems = items.filter((item) => {
// Check if all words in the query are present in the item
return queryWords.every((word) => item.includes(word));
});
const end = performance.now();
console.log(`IndexOf Method: ${end - start} milliseconds`);
};
// Approach using indexOf
const benchmarkIncludes = (items, query) => {
const start = performance.now();
const queryWords = query.toLowerCase().split(' ');
const filteredItems = items.filter((item) => {
// Check if all words in the query are present in the item
return queryWords.every((word) => item.includes(word));
});
const end = performance.now();
console.log(`Includes Method: ${end - start} milliseconds`);
};
Ready to run.
Test | Ops/sec | |
---|---|---|
Test Regex |
| ready |
Test Includes |
| ready |
Test IndexOf |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.