Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:46:07+00:00 2026-06-13T23:46:07+00:00

The example app for benchmark.js defines its performance tests as strings rather than JavaScript

  • 0

The example app for benchmark.js defines its performance tests as strings rather than JavaScript functions:

https://github.com/bestiejs/benchmark.js/blob/master/example/jsperf/index.html#L250

Is there an advantage to defining the performance test in this way? Why isn’t it just a function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:46:09+00:00Added an answer on June 13, 2026 at 11:46 pm

    From what I can tell, through a long series of calls the function code there ends up at a function in benchmark.js named getSource, which converts it to a string anyway:

    /**
     * Gets the source code of a function.
     *
     * @private
     * @param {Function} fn The function.
     * @param {String} altSource A string used when a function's source code is unretrievable.
     * @returns {String} The function's source code.
     */
    function getSource(fn, altSource) {
      var result = altSource;
      if (isStringable(fn)) {
        result = String(fn);
      } else if (support.decompilation) {
        // escape the `{` for Firefox 1
        result = (/^[^{]+\{([\s\S]*)}\s*$/.exec(fn) || 0)[1];
      }
      // trim string
      result = (result || '').replace(/^\s+|\s+$/g, '');
    
      // detect strings containing only the "use strict" directive
      return /^(?:\/\*+[\w|\W]*?\*\/|\/\/.*?[\n\r\u2028\u2029]|\s)*(["'])use strict\1;?$/.test(result)
        ? ''
        : result;
    }
    

    This is done within the function “clock”, which offers another explanation down the line as to its motiviations to making a benchmark function out of a string each time clock is called:

    // Compile in setup/teardown functions and the test loop.
    // Create a new compiled test, instead of using the cached `bench.compiled`,
    // to avoid potential engine optimizations enabled over the life of the test.
    

    I am no expert of benchmark.js, but my reading of it is that the library deliberately wants to start the benchmark from raw code, forcing the compiler/interpreter to run on code it’s never seen before. This does make sense to me, as a hypothetical browser that freezes for five seconds on load to optimize the code might have amazing performance, but it would be unfair to ignore the five seconds it spent compiling in the benchmark. Also, it does a fair bit of modification to the original code in order to control the scope and exception handling; benchmark just needs the code as a string in order to properly manipulate it.

    In this view, it isn’t so much that passing the functions as strings is particularly important, but there’s no reason not to – it’s going to end up as a string anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the MWFeedParser (found here: https://github.com/mwaterfall/MWFeedParser ). I followed his example app,
I'm trying to figure out backbone from an example app (see https://github.com/elfsternberg/The-Backbone-Store ). The
Sorcery authentication gem: https://github.com/NoamB/sorcery Sorcery's creator provides an example Rails app with Sorcery test
What are the benefits of http://www.example.com/app/servlet/cat1/cat2/item URL over http://www.example.com/app/servlet?catid=12345 URL Could there be any
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html I was going to add this but how can i add it if
Using the sample app for rpx_now gem ( http://github.com/grosser/rpx_now_example ) on localhost:3000, I have
If Android app with AdMob banner has site URL market://details?id=<com.example.app> Will it work for
I have an app for example: app.com And I would like to have some
I am getting javax.servlet.ServletException: Failed to load application class: com.example.App when i try to
I am making an example app of a game. I have a folder i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.