sopt 54600

JavaScript performance comparison

Test case created by bfavaretto

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  var obj = {"frutas": 50, "vegetais": 100, "carnes": 150 };

};
</script>

Preparation code output

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
bfavaretto
var maior = -Infinity;
var chave;
for(var prop in obj) {
    // ignorar propriedades herdadas
    if(obj.hasOwnProperty(prop)) { 
         if(obj[prop] > maior) {
             maior = obj[prop];
             chave = prop;
         }
    }
}
pending…
mgibsonbr
var maior = Object.keys(obj).sort(function(a,b) {
                return obj[a] > obj[b] ? -1 :
                       obj[b] > obj[a] ? 1 : 0;
            })[0];
pending…
Luis Henrique (jQuery)
  // Variável que armazenará a maior key.
  var key;
  // Variável auxiliar para checar os valores
  var value = 0;
  jQuery.each(obj, function(idx, val){
    // Se o valor atual da iteração é maior que o auxiliar
    if(val > value){
      // Atualiza o valor da variável auxiliar
      value = val;
      // Atribue a nova key
      key = idx;
    }
  });
  return key;
pending…

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

Compare results of other browsers

0 Comments