HTML Preparation code:
AخA
 
1
<div id='test'></div>
Script Preparation code:
x
 
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)
  }, '');
}
Tests:
  • for...of - 1000 short cookies

     
    return getCookieForOf('findMe', documentCookie1000);
  • IndexOf() - 1000 short cookies

     
    return getCookieIndexOf('findMe', documentCookie1000);
  • find() - 1000 short cookies

     
    return getCookieFind('findMe', documentCookie1000);
  • reduce() - 1000 short cookies

     
    return getCookieReduce('findMe', documentCookie1000);
  • for...of - 10 long cookies

     
    return getCookieForOf('findMe', documentCookie10);
  • IndexOf() - 10 long cookies

     
    return getCookieIndexOf('findMe', documentCookie10);
  • find() - 10 long cookies

     
    return getCookieFind('findMe', documentCookie10);
  • reduce() - 10 long cookies

     
    return getCookieReduce('findMe', documentCookie10);
  • for - 1000 short cookies

     
    return getCookieFor('findMe', documentCookie1000);
  • for - 10 long cookies

     
    return getCookieFor('findMe', documentCookie10);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • 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

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2 months ago)
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36
Chrome Mobile 131 on Android
View result in a separate tab
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