multiline (v4)

Revision 4 of this benchmark created on


Description

Testing the performance of the multiline module. This is only a naive micro-benchmark. Please treat it as such.

https://github.com/sindresorhus/multiline

Setup

/*!
        multiline
        Multiline strings in JavaScript
        https://github.com/sindresorhus/multiline
        by Sindre Sorhus
        MIT License
    */
    (function() {
      'use strict';
    
      // start matching after: comment start block => optional whitespace => newline
      // stop matching before: last newline => optional whitespace => comment end block
      var reCommentContents = /\/\*\s*(?:\r\n|\n)([\s\S]*?)(?:\r\n|\n)\s*\*\//;
    
      var multiline = function(fn) {
        if (typeof fn !== 'function') {
          throw new TypeError('Expected a function.');
        }
    
        return reCommentContents.exec(fn.toString())[1];
      };
    
      if (typeof module !== 'undefined' && module.exports) {
        module.exports = multiline;
      } else {
        window.multiline = multiline;
      }
    })();
    
    window.multi = function(fn) {
      if (typeof fn !== 'function') {
        throw new TypeError('Expected a function.');
      }
      fn = fn.toString();
      return fn.substring(fn.indexOf('\n') + 1, fn.lastIndexOf('\n'));
    }

Test runner

Ready to run.

Testing in
TestOps/sec
multiline
var str = multiline(function() {
  /*
<!doctype html>
<html>
        <body>
                <h1>Hello world</h1>
        </body>
</html>
*/
});
ready
string concat
var str =
  '<!doctype html>' +
  '<html>' +
  '     <body>' +
  '             <h1>Hello world</h1>' +
  '     </body>' +
  '</html>';
ready
escaped new lines
var str = '<!doctype html>\
<html>\
        <body>\
                <h1>Hello world</h1>\
        </body>\
</html>';
ready
not regex
var str = multi(function() {
  /*
<!doctype html>
<html>
        <body>
                <h1>Hello world</h1>
        </body>
</html>
*/
});
ready
Array join
var str = ['<!doctype html>',
  '<html>',
  '     <body>',
  '             <h1>Hello world</h1>',
  '     </body>',
  '</html>'
].join('');
ready

Revisions

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