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 rawParams = {
country_code: 'US',
city: 'New York',
postal_code: '10001',
region_code: 'NY',
other_key: 'Some Value',
null_key: null
};
function hashString({ rawString, algorithm }) {
return new Promise((resolve) => {
setTimeout(() => resolve(`${algorithm}_${rawString}`), Math.random() * 100);
});
}
async function processParamsWithPromiseAll(params) {
return Promise.all(
Object.entries(params).map(async ([key, value]) => {
if (key === 'country_code' && typeof value === 'string') {
return ['country', await hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })];
}
if (key === 'city' && typeof value === 'string') {
return ['ct', await hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })];
}
if (key === 'postal_code' && typeof value === 'string') {
return ['zp', await hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })];
}
if (key === 'region_code' && typeof value === 'string') {
return ['st', await hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })];
}
if (value != null) return [key, value];
return [key, null];
})
);
}
async function processParamsWithPromiseAllAndArrayOfPromises(params) {
return Promise.all(
Object.entries(rawParams).map(([key, value]) => {
let transformedKey = key;
let valuePromise;
if (key === "country_code" && typeof value === "string") {
transformedKey = "country";
valuePromise = hashString({
rawString: value.trim().toLowerCase(),
algorithm: "sha256",
});
} else if (key === "city" && typeof value === "string") {
transformedKey = "ct";
valuePromise = hashString({
rawString: value.trim().toLowerCase().replace(/\s+/g, ""),
algorithm: "sha256",
});
} else if (key === "postal_code" && typeof value === "string") {
transformedKey = "zp";
valuePromise = hashString({
rawString: value.trim().toLowerCase(),
algorithm: "sha256",
});
} else if (key === "region_code" && typeof value === "string") {
transformedKey = "st";
valuePromise = hashString({
rawString: value.trim().toLowerCase().replace(/\s+/g, ""),
algorithm: "sha256",
});
} else {
valuePromise = Promise.resolve(value);
}
return valuePromise.then((resolvedValue) => ({
[transformedKey]: resolvedValue,
}));
})
);
}
async function processParamsWithPromiseAllnoAwait(params) {
return Promise.all(
Object.entries(params).map(([key, value]) => {
if (key === 'country_code' && typeof value === 'string') {
return ['country', hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })];
}
if (key === 'city' && typeof value === 'string') {
return ['ct', hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })];
}
if (key === 'postal_code' && typeof value === 'string') {
return ['zp', hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })];
}
if (key === 'region_code' && typeof value === 'string') {
return ['st', hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })];
}
if (value != null) return [key, value];
return [key, null];
})
);
}
async function processParamsSequential(params) {
const out = [];
for (const [key, value] of Object.entries(params)) {
if (key === 'country_code' && typeof value === 'string') {
out.push(['country', await hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })]);
} else if (key === 'city' && typeof value === 'string') {
out.push(['ct', await hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })]);
} else if (key === 'postal_code' && typeof value === 'string') {
out.push(['zp', await hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' })]);
} else if (key === 'region_code' && typeof value === 'string') {
out.push(['st', await hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' })]);
} else if (value != null) {
out.push([key, value]);
} else {
out.push([key, null]);
}
}
return out;
}
// Create an array of promises that handle all transformations
const transformationPromises = Object.entries(rawParams).map(([key, value]) => {
let transformedKey = key;
let valuePromise;
if (key === 'country_code' && typeof value === 'string') {
transformedKey = 'country';
valuePromise = hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' });
} else if (key === 'city' && typeof value === 'string') {
transformedKey = 'ct';
valuePromise = hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' });
} else if (key === 'postal_code' && typeof value === 'string') {
transformedKey = 'zp';
valuePromise = hashString({ rawString: value.trim().toLowerCase(), algorithm: 'sha256' });
} else if (key === 'region_code' && typeof value === 'string') {
transformedKey = 'st';
valuePromise = hashString({ rawString: value.trim().toLowerCase().replace(/\s+/g, ''), algorithm: 'sha256' });
} else {
// For other cases, resolve immediately without waiting for a promise
valuePromise = Promise.resolve(value);
}
// Return the promise along with its transformed key
return valuePromise.then((resolvedValue) => ({ [transformedKey]: resolvedValue }));
});
async function processBatchedPromises(params) {Promise.all(params);}
Ready to run.
| Test | Ops/sec | |
|---|---|---|
| Promise.all with await on hashString | | ready |
| Promise.all no await on hashString | | ready |
| no Promise.all just await on hashString | | ready |
| Promise.all array of promises | | ready |
| Promise.all batch promises | | ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.