Skip values in a for-loop

JavaScript performance comparison

Revision 3 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function formatTitle(s) {
    return s;
  }
  
  function arrayPushUnknown(columns, keyTitle, boolean, key) {
    columns[key] = keyTitle;
  }
  
  var arr = {
    ContentType: 'text/plain',
    FileRef: 'fileref',
    ContentLength: 31415,
    FileName: 'asdf.txt',
    SourceURL: 'http://codereview.stackexchange.com/questions/32486/optimal-way-of-skipping-certain-values-in-a-for-loop',
    Misc: 'these are silly values for testing',
  };

};
</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
if not foo and not bar
var columns = {};

for (var key in arr) {
  if (key != 'ID' && key != 'ContentType' && key != 'FileLeafRef' && key != 'FileRef' && key != 'UniqueId' && key != 'PermMask' && key != 'owshiddenversion' && key != 'Created' && key != 'FSObjType' && key != '_Level' && key != '_ModerationStatus') {
    arrayPushUnknown(columns, formatTitle(key), true, key)
  }
}
pending…
switch
var columns = {};

for (var key in arr) {
  switch (key) {
    case 'ID':
    case 'ContentType':
    case 'FileRef':
    case 'FileLeafRef':
    case 'UniqueId':
    case 'PermMask':
    case 'owshiddenversion':
    case 'Created':
    case 'FSObjType':
    case '_Level':
    case '_ModerationStatus':
      continue;
    default:
      arrayPushUnknown(columns, formatTitle(key), true, key);
  }
}
pending…

Compare results of other browsers

Revisions

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

0 Comments