Creating objects inside a function vs outside a function

Benchmark created on


Setup

const EMPTY_CONTENT = {
  showSavingsCalculatorBanner: false,
  bannerContent: {
    label: "",
    byline: "",
    description: "",
  },
  cardContent: {
    label: "",
    byline: "",
    description: "",
  },
  comparisons: [
    {
      label: "",
      sections: {},
    },
  ],
};

Test runner

Ready to run.

Testing in
TestOps/sec
Object created in function
function doSomething() {
  const EMPTY_CONTENT = {
    showSavingsCalculatorBanner: false,
    bannerContent: {
      label: "",
      byline: "",
      description: "",
    },
    cardContent: {
      label: "",
      byline: "",
      description: "",
    },
    comparisons: [
      {
        label: "",
        sections: {},
      },
    ],
  };

  if (false) {
    return EMPTY_CONTENT;
  }
  return {
    showSavingsCalculatorBanner: false,
    bannerContent: {
      label: "",
      byline: "",
      description: "",
    },
    cardContent: {
      label: "",
      byline: "",
      description: "",
    },
    comparisons: [
      {
        label: "",
        sections: {},
      },
    ],
  };
}

doSomething();
ready
Object created outside a function
function doSomething() {
  if (false) {
    return EMPTY_CONTENT;
  }
  return {
    showSavingsCalculatorBanner: false,
    bannerContent: {
      label: "",
      byline: "",
      description: "",
    },
    cardContent: {
      label: "",
      byline: "",
      description: "",
    },
    comparisons: [
      {
        label: "",
        sections: {},
      },
    ],
  };
}

doSomething()
ready

Revisions

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