Get Strings from Obj

Benchmark created on


Description

alternatives to JSON Stringify

Preparation HTML


Setup

function getAllStrings(obj) {
  const strings = [];

  function traverse(obj) {
    for (const key in obj) {
      const value = obj[key];

      if (typeof value === 'string') {
        strings.push(value);
      } else if (typeof value === 'object') {
        traverse(value);
      }
    }
  }

  traverse(obj);

  return strings;
}

function getAllStrings2(obj) {
  const strings = [];

  function traverse(obj) {
    Object.keys(obj).forEach(k => {
      const value = obj[k];

      if (typeof value === 'string') {
        strings.push(value);
      } else if (value !== null && typeof value === 'object') {
        traverse(value);
      }
    })

  }

  traverse(obj);

  return strings;
}


function getAllStrings3(obj) {
  const strings = [];

  function traverse(obj) {

    Object.values(obj).forEach(value => {
      if (typeof value === 'string') {
        strings.push(value);
      } else if (value !== null && typeof value === 'object') {
        traverse(value);
      }
    })

  }

  traverse(obj);

  return strings;
}
const gptWithFB = {
  metadata: {
    tags: [],
    legal: {
      'wifi-6-3rd-party-testing-benefits': {
        index: 0,
        key: 'wifi-6-3rd-party-testing-benefits',
        value:
          'Based on third party testing that compared the upload and download throughput speeds of TELUS and its major competitor’s Wi-Fi 6 devices. Benefits of Wi-Fi 6 are only experienced on Wi-Fi 6 enabled devices. For more information on Wi-Fi 6 capabilities visit <a href="https://www.telus.com/WiFi">telus.com/WiFi</a>.',
      },
    },
  },
  sys: {
    space: { sys: { type: 'Link', linkType: 'Space', id: 'fltupc9ltp8m' } },
    type: 'Entry',
    id: '7aXMCzH3cAnoTRAio8DyOn',
    contentType: { sys: { type: 'Link', linkType: 'ContentType', id: 'genericPageTemplate' } },
    revision: 2,
    createdAt: '2022-09-30T20:48:07.489Z',
    updatedAt: '2022-10-19T22:13:51.700Z',
    environment: { sys: { id: 'sandbox', type: 'Link', linkType: 'Environment' } },
    locale: 'en-CA-AB',
  },
  fields: {
    entryTitle: 'Analytics Form Page',
    slug: 'test/cjeronimo-test',
    regions: ['ab', 'bc'],
    breadcrumbs: {
      sys: { contentType: { sys: { id: 'breadcrumbs' } } },
      fields: {
        breadcrumb: [
          { sys: { id: 'breadcrumb-1' }, fields: { label: 'Home', slug: '/' } },
          { sys: { id: 'breadcrumb-2' }, fields: { label: 'Security', slug: '/security' } },
          { sys: { id: 'breadcrumb-3' }, fields: { label: 'Products', slug: '/security/products' } },
        ],
      },
    },
    seo: {
      metadata: { tags: [] },
      sys: {
        space: { sys: { type: 'Link', linkType: 'Space', id: 'fltupc9ltp8m' } },
        type: 'Entry',
        id: '14pbZjLsnQObFmKtK2bc5E',
        contentType: { sys: { type: 'Link', linkType: 'ContentType', id: 'seoTags' } },
        revision: 1,
        createdAt: '2022-09-30T17:40:02.053Z',
        updatedAt: '2022-09-30T17:44:37.778Z',
        environment: { sys: { id: 'sandbox', type: 'Link', linkType: 'Environment' } },
        locale: 'en-CA-AB',
      },
      fields: {
        entryTitle: 'SB2 Alloy Automation POC',
        title: 'Alloy Automation POC - Alloy Automation POC - Alloy Automation POC',
        description:
          'Alloy Automation POC - Alloy Automation POC - Alloy Automation POC - Alloy Automation POC - Alloy Automation POC',
        keywords: ['SB2'],
        ogImage: {
          metadata: { tags: [] },
          sys: {
            space: { sys: { type: 'Link', linkType: 'Space', id: 'fltupc9ltp8m' } },
            type: 'Asset',
            id: '2BOlSVAJIlXMUnINVvI1tg',
            revision: 1,
            createdAt: '2022-09-30T17:43:14.740Z',
            updatedAt: '2022-09-30T17:44:29.950Z',
            environment: { sys: { id: 'sandbox', type: 'Link', linkType: 'Environment' } },
            locale: 'en-CA-AB',
          },
          fields: {
            title: 'Screen Shot 2020-09-30 at 11.09.45 AM',
            description: '',
            file: {
              url: '//images.ctfassets.net/fltupc9ltp8m/2BOlSVAJIlXMUnINVvI1tg/08c0ab947149786e18673a53cc8686dd/Screen_Shot_2020-09-30_at_11.09.45_AM.png',
              details: { size: 136312, image: { width: 1894, height: 638 } },
              fileName: 'Screen_Shot_2020-09-30_at_11.09.45_AM.png',
              contentType: 'image/png',
            },
          },
        },
      },
    },
    dataLayer: {
      metadata: { tags: [] },
      sys: {
        space: { sys: { type: 'Link', linkType: 'Space', id: 'fltupc9ltp8m' } },
        type: 'Entry',
        id: '4JnLs9HL74krHoQfCnAspo',
        contentType: { sys: { type: 'Link', linkType: 'ContentType', id: 'pageDataLayer' } },
        revision: 1,
        createdAt: '2022-09-30T17:45:11.412Z',
        updatedAt: '2022-09-30T17:47:00.452Z',
        environment: { sys: { id: 'sandbox', type: 'Link', linkType: 'Environment' } },
        locale: 'en-CA-AB',
      },
      fields: {
        entryTitle: 'SB2 Alloy Automation POC',
        pageType: 'homepage',
        category: {
          metadata: { tags: [] },
          sys: {
            space: { sys: { type: 'Link', linkType: 'Space', id: 'fltupc9ltp8m' } },
            type: 'Entry',
            id: '1ssjFDDhAVryOAXbTxFReZ',
            contentType: { sys: { type: 'Link', linkType: 'ContentType', id: 'dataLayerCategory' } },
            revision: 1,
            createdAt: '2022-09-30T17:46:02.243Z',
            updatedAt: '2022-09-30T17:46:16.869Z',
            environment: { sys: { id: 'sandbox', type: 'Link', linkType: 'Environment' } },
            locale: 'en-CA-AB',
          },
          fields: { entryTitle: 'home', analyticsRef: 'home' },
        },
        subcategory: [
          {
            fields: { entryTitle: 'home', analyticsRef: 'SubHome' },
          },
          {
            fields: { entryTitle: 'home', analyticsRef: 'SubAbout' },
          },
        ],
        dataLayerCartEvent: {
          fields: { entryTitle: 'home', cartEventName: 'Cart Name' },
        },
        dataLayerProduct: {
          fields: {
            entryTitle: 'Fake Product',
            name: 'test product name',
            category: 'test',
            price: '100',
            sku: '12345',
            slug: '/fake/product',
          },
        },
      },
    },
    alloyDataLayer: {
      metadata: { tags: [] },
      sys: {
        space: { sys: { type: 'Link', linkType: 'Space', id: 'fltupc9ltp8m' } },
        type: 'Entry',
        id: '2wI0s545cZbYRd0hFq5ZxU',
        contentType: { sys: { type: 'Link', linkType: 'ContentType', id: 'pageAlloyDataLayer' } },
        revision: 2,
        createdAt: '2022-09-30T17:49:57.491Z',
        updatedAt: '2022-10-03T21:23:18.057Z',
        environment: { sys: { id: 'sandbox', type: 'Link', linkType: 'Environment' } },
        locale: 'en-CA-AB',
      },
      fields: {
        entryTitle: 'POC Alloy adobeDataLayer',
        pageType: 'landing',
        siteSection: 'health',
        primarySiteSection: 'first',
        secondarySiteSection: 'second',
        tertiarySiteSection: 'third',
        quaternarySiteSection: 'fourth',
        lineOfBusiness: ['brand_experience', 'business'],
        contentName: 'test content',
      },
    },
    blocks: [
      {
        metadata: { tags: [] },
        sys: {
          space: { sys: { type: 'Link', linkType: 'Space', id: 'fltupc9ltp8m' } },
          type: 'Entry',
          id: 'PDQb83Ey9ig6X6CD0c10p',
          contentType: {
            sys: {
              id: 'row',
              linkType: 'ContentType',
              type: 'Link',
            },
          },
          locale: 'en-CA-AB',
          createdAt: '2022-04-08T11:50:30.071Z',
          updatedAt: '2022-05-27T13:52:56.562Z',
        },
        fields: {
          entryTitle: 'Row Test',
          children: [
            {
              sys: {
                id: 'id1',
                type: 'Entry',
                contentType: {
                  sys: {
                    id: 'column',
                    linkType: 'ContentType',
                    type: 'Link',
                  },
                },
                locale: 'en-CA-AB',
                createdAt: '2022-04-08T11:50:30.071Z',
                updatedAt: '2022-05-27T13:52:56.562Z',
              },
              fields: {
                entryTitle: 'Column test',
                components: [
                  {
                    sys: {
                      id: 'id1',
                      type: 'Entry',
                      contentType: {
                        sys: {
                          id: 'text',
                          linkType: 'ContentType',
                          type: 'Link',
                        },
                      },
                      locale: 'en-CA-AB',
                      createdAt: '2022-04-08T11:50:30.071Z',
                      updatedAt: '2022-05-27T13:52:56.562Z',
                    },
                    fields: {
                      entryTitle: 'Test text',
                      text: 'Text text',
                    },
                  },
                ],
              },
            },
          ],
        },
      },
    ],
    visitorState: {},
  },
}

Test runner

Ready to run.

Testing in
TestOps/sec
For in Loop
getAllStrings(gptWithFB)
ready
Keys For Each
getAllStrings2(gptWithFB)
ready
Values For Each
getAllStrings3(gptWithFB)
ready
JSON Stringify
JSON.stringify(gptWithFB)
ready

Revisions

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