Skip values in a for-loop

JavaScript performance comparison

Test case created by 200_success

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
switch
var columns = {};

for (var key in arr) {
    switch (key) {
      case 'ContentType':
      case 'FileRef':
        continue;
      default:
        arrayPushUnknown(columns, formatTitle(key), true, key);
    }
}
pending…
if not foo and not bar
var columns = {};

for (var key in arr ) {
    if ( key != 'ContentType' && key != 'FileRef' ) {
        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