template-literal-vs-loop

JavaScript performance comparison

Test case created by dy

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let re = /\0/g
  function repl () {this.shift() ?? ''}
  const tpl = (s, ...fields) => s.join('\0').replace(re, repl.bind(fields))
  
  const loop = (s, ...fields) => {
    let i = 0, n = s.length, parts = []
    while (i++ < n) parts.push(s[i], fields.shift() ?? '')
    parts.push(s[i])
    return parts.join('')
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
template
tpl`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Orci eu lobortis elementum nibh ${null} molestie nunc non blandit. ${1} tincidunt id aliquet risus feugiat in. Arcu non odio euismod lacinia at quis. Pulvinar pellentesque habitant morbi ${0} senectus et netus. Et ultrices neque ornare aenean euismod elementum nisi quis. Elit sed ${false} mi sit amet. Nibh ${'bar'} id aliquet lectus proin. ${undefined} aliquam ut porttitor leo a diam sollicitudin tempor id. Urna duis convallis convallis tellus id interdum velit laoreet id. Odio eu feugiat ${'baz'} nibh ipsum consequat. Nam libero justo laoreet sit amet cursus sit amet dictum. Nisl purus in mollis ${'qux'}. Congue nisi vitae suscipit tellus mauris a diam. Urna molestie at elementum eu facilisis sed.`
pending…
loop
loop`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Orci eu lobortis elementum nibh ${null} molestie nunc non blandit. ${1} tincidunt id aliquet risus feugiat in. Arcu non odio euismod lacinia at quis. Pulvinar pellentesque habitant morbi ${0} senectus et netus. Et ultrices neque ornare aenean euismod elementum nisi quis. Elit sed ${false} mi sit amet. Nibh ${'bar'} id aliquet lectus proin. ${undefined} aliquam ut porttitor leo a diam sollicitudin tempor id. Urna duis convallis convallis tellus id interdum velit laoreet id. Odio eu feugiat ${'baz'} nibh ipsum consequat. Nam libero justo laoreet sit amet cursus sit amet dictum. Nisl purus in mollis ${'qux'}. Congue nisi vitae suscipit tellus mauris a diam. Urna molestie at elementum eu facilisis sed.`
pending…

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

0 Comments