Chaining vs variable (v2)

Revision 2 of this benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Variable
const createPageTitle = ({ slug, afterSlugText, pageNumber, siteName }) => {
  const title = [slug, afterSlugText, pageNumber > 1 && `Page ${pageNumber}`]
    .filter(Boolean)
    .join(' - ');

  return `${title} | ${siteName}`;
}

createPageTitle({ slug: 'slug', afterSlugText: 'test', pageNumber: 2, siteName: 'test'})
ready
Clean chain
const createPageTitle = ({ slug, afterSlugText, pageNumber, siteName }) =>
  [slug, afterSlugText, pageNumber > 1 && `Page ${pageNumber}`]
    .filter(Boolean)
    .join(' - ')
    .split()
    .concat(siteName)
    .join(' | ');
 
createPageTitle({ slug: 'slug', afterSlugText: 'test', pageNumber: 2, siteName: 'test'})
ready
Avoid split
const createPageTitle = ({ slug, afterSlugText, pageNumber, siteName }) =>
  [
    [slug, afterSlugText, pageNumber > 1 && `Page ${pageNumber}`].filter(Boolean).join(' - '),
    siteName,
  ].join(' | ');
  
 createPageTitle({ slug: 'slug', afterSlugText: 'test', pageNumber: 2, siteName: 'test'})
ready
Straight to backtics
const createPageTitle = ({ slug, afterSlugText, pageNumber, siteName }) =>
  `${[slug, afterSlugText, pageNumber > 1 && `Page ${pageNumber}`]
    .filter(Boolean)
    .join(' - ')} | ${siteName}`;
    
createPageTitle({ slug: 'slug', afterSlugText: 'test', pageNumber: 2, siteName: 'test'})
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.