<div id='test'></div>
let values1000 = [Array(1000).keys()];
var documentCookie1000 = ''; // mock document.cookie string - 1000 SHORT VALUES
values1000.forEach(v => {
documentCookie1000 += `${v}name=${v}some-unique-cookie-value-with=equal-sign; `;
})
documentCookie1000 += 'findMe=found-test-value-with=equal-sign; lastCookie=test-value';
console.log(documentCookie1000);
let values10 = [Array(10).keys()];
var documentCookie10 = ''; // mock document.cookie string - 10 LONG VALUES
values10.forEach(v => {
documentCookie10 += `${v}name=${v}511=RmKsfdVddsfsdmrAQi8UK_S8CF7Bsdfds=O9lh5lAa74bsuEu31d3hB-_3wcypj6fdssqWzo-DbjOKnHHL6bkiEt6dsfZHrEXiFuuKTq2kdlOtE9up4EFdsfsdk90XLwjT1Up7maWRYT8EHm3eluUoQ0KHLEjpSfjFjiY75b8XRHWuNMcw9j4vLaQU3CcfIY-GMRz9DhYF1dsfsdec8PeJj9Mg9do_7ziOcdsfdsDrmLXj3P69RWkIVMpXh_iYQ2bpPWW7zAq0Vqkdfdq7fqYZYsARIz57JXJwGnezDrLjbnFEJNnCfFW52jqHYJlfKpmdfdhPp9DZzfdDCoXDuQ1KRDRgAxmfukkZcMIbdOWcooD41lgezwxpaXXuvYrJTpJhJd5iifSd1STFyxMeQq6y9mIBuc-Q1FXVKqVzu1KmMvRozzqz3bt95zUKmEdGGUkdcPdE8n11cDMdJutpNhEm2gMVcKmKAcEtt-5qOCbOjmqdoFd-qdSF7b19v8Zdsfds8oOf5LPQjB8kU1WlwrEJ59KAkMEyEBjGfVZMkbvxdfdsaLxLgOHqEWrHZoZjrnMMEOgoarCviw7qCD4OqabPhdsfdsbh9UVwJelx_cVddfdsf9GetyLbPy1PFdfOgT; `;
})
documentCookie10 += 'findMe=found-test-value-with=equal-sign; lastCookie=test-value';
console.log(documentCookie10);
function getCookieFor(name, cookies) {
const nameEQ = name + "=";
const ca = cookies.split('; ');
for(let i=0;i < ca.length;i++) {
const c = ca[i];
if (c.indexOf(nameEQ) === 0) {
const value = c.substring(nameEQ.length);
return decodeURIComponent(value); // returns first found cookie
}
}
return null;
}
function getCookieForOf(name, cookies) {
const nameEQ = name + '=';
for (const cookie of cookies.split('; ')) {
if (cookie.indexOf(nameEQ) === 0) {
const value = cookie.substring(nameEQ.length);
return decodeURIComponent(value); // returns first found cookie
}
}
return null;
}
function getCookieIndexOf(name, cookies) {
const nameEQ = name + '=';
const cookieStart = cookies.indexOf(nameEQ);
if (cookieStart !== -1) {
const cookieValueStart = cookieStart + nameEQ.length;
const cookieEnd = cookies.indexOf(';', cookieValueStart);
const value = cookies.substring(
cookieValueStart,
cookieEnd !== -1 ? cookieEnd : undefined
);
return decodeURIComponent(value); // returns first found cookie
}
return null;
}
function getCookieFind(name, cookies) {
const nameEQ = name + '=';
const foundCookie = cookies
.split('; ')
.find(c => c.indexOf(nameEQ) === 0); // returns first found cookie
if (foundCookie) {
return decodeURIComponent(foundCookie.substring(nameEQ.length));
}
return null;
}
function getCookieReduce(name, cookies) {
return cookies.split('; ').reduce((r, v) => {
const [n, val] = v.split('='); // cookie value can contain "="
if(r) return r; // returns first found cookie
return n === name ? decodeURIComponent(val.join('=')) : r; // returns last found cookie (overwrites)
}, '');
}
return getCookieForOf('findMe', documentCookie1000);
return getCookieIndexOf('findMe', documentCookie1000);
return getCookieFind('findMe', documentCookie1000);
return getCookieReduce('findMe', documentCookie1000);
return getCookieForOf('findMe', documentCookie10);
return getCookieIndexOf('findMe', documentCookie10);
return getCookieFind('findMe', documentCookie10);
return getCookieReduce('findMe', documentCookie10);
return getCookieFor('findMe', documentCookie1000);
return getCookieFor('findMe', documentCookie10);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for...of - 1000 short cookies | |
IndexOf() - 1000 short cookies | |
find() - 1000 short cookies | |
reduce() - 1000 short cookies | |
for...of - 10 long cookies | |
IndexOf() - 10 long cookies | |
find() - 10 long cookies | |
reduce() - 10 long cookies | |
for - 1000 short cookies | |
for - 10 long cookies |
Test name | Executions per second |
---|---|
for...of - 1000 short cookies | 9849.0 Ops/sec |
IndexOf() - 1000 short cookies | 291749.7 Ops/sec |
find() - 1000 short cookies | 11324.3 Ops/sec |
reduce() - 1000 short cookies | 2396.4 Ops/sec |
for...of - 10 long cookies | 77731.6 Ops/sec |
IndexOf() - 10 long cookies | 147955.3 Ops/sec |
find() - 10 long cookies | 137140.6 Ops/sec |
reduce() - 10 long cookies | 139977.7 Ops/sec |
for - 1000 short cookies | 11020.4 Ops/sec |
for - 10 long cookies | 138693.0 Ops/sec |
A JSON data dump!
Let's dive into the data. It appears to be a collection of test results for various JavaScript operations on arrays of cookies.
Here are some key observations:
TestName
.Browser
: The browser type and version.DevicePlatform
: The device platform (Desktop).OperatingSystem
: The operating system used.ExecutionsPerSecond
: The number of executions per second for this test.- 10 long cookies
) and short (- 1000 short cookies
).Some interesting questions arise from this data:
Feel free to ask me specific questions about the data, and I'll do my best to provide insights!