Fast deep checking

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var person = {
        firstName: 'Ron',
        lastName: 'Swanson',
        likes: {
            food: {
                meat: 'bacon',
                drink: 'scotch'
            },
            instrument: 'saxaphone'
        },
        saySomething: function() { return 'No'; }
    }
   
   
    var nullet = {
      get: function(object, namespace, defaultVal) {
        if (object == null) return defaultVal;
   
        var names = namespace.split('.').reverse();
   
        while(names.length && (object = object[names.pop()]) != null);
   
        return(object == null ? defaultVal : object);
      }
    }
   
   
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Try Catch
var nameOfPerson     
  , favoriteMeat    
  , favoriteIcecream
  , promptResult    
  , soundResult      
  ;

try {
  nameOfPerson     = person.firstName;
} catch(err) {
  nameOfPerson = 'No Name';
}
try {
  favoriteMeat     = person.likes.food.meat;
} catch(err) {
  favoriteMeat = 'No favorite meat';
}
try {
  favoriteIcecream = person.likes.food.icecream;
} catch(err) {
  favoriteIcecream = 'No favorite icecream'
}
try {
  promptResult = person.saySomething();
} catch(err) {
  promptResult = function() { return 'No response'; }()
}
try {
  soundResult = person.barkLikeADog();
} catch(err) {
  soundResult = function() { return 'Would not bark like a dog' }();
}
pending…
Nullet
var nameOfPerson     = nullet.get(person, 'firstName', 'No Name')
  , favoriteMeat     = nullet.get(person, 'likes.food.meat', 'No favorite meat')
  , favoriteIcecream = nullet.get(person, 'likes.food.icecream', 'No favorite icecream')
  , promptResult     = nullet.get(person, 'saySomething', function() { return 'No response'; })()
  , soundResult      = nullet.get(person, 'barkLikeADog', function() { return 'Would not bark like a dog' })()
  ;
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. Here’s a list of current revisions for this page:

0 comments

Add a comment