parameterCount (v2)

Revision 2 of this benchmark created on


Setup

const str1 = 'foo&bar&baz';
const str2 = 'foo&bar&baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz';
const str3 = 'foo&bar&baz&lol&rofl&lmao';
const str4 = 'foo&bar&baz&lol&rofl&lmao&omg&wtf&wut&foo&bar&baz&lol&rofl&lmao&omg&wtf&wut&foo&bar&baz&lol&rofl&lmao&omg&wtf&wut&foo&bar&baz&lol&rofl&lmao&omg&wtf&wut&foo&bar&baz&lol&rofl&lmao&omg&wtf&wut&foo&bar&baz&lol&rofl&lmao&omg&wtf&wut&foo&bar&baz&lol&rofl&lmao&omg&wtf&wut&foo&bar&baz&lol&rofl&lmao&omg&wtf&wut&foo&bar&baz&lol&rofl&lmao&omg&wtf&wut&foo&bar&baz&lol&rofl&lmao&omg&wtf&wut';

Test runner

Ready to run.

Testing in
TestOps/sec
current
function parameterCount (body, limit) {
  var count = 0
  var index = 0

  while ((index = body.indexOf('&', index)) !== -1) {
    count++
    index++

    if (count === limit) {
      return undefined
    }
  }

  return count
}

parameterCount(str1, 100)
parameterCount(str2, 100)
parameterCount(str3, 3)
parameterCount(str4, 3)
ready
proposed
function parameterCount (body, limit) {
  var len = body.split('&').length;
  return len > limit ? undefined : len - 1;
}

parameterCount(str1, 100)
parameterCount(str2, 100)
parameterCount(str3, 3)
parameterCount(str4, 3)
ready

Revisions

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