leafy vs mixin

JavaScript performance comparison

Test case created by Anders

Preparation code

<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var leafy=function(e){"use strict";var t=function(){function c(e){return Object.prototype.toString.call(e)==="[object Object]"}function h(e){return Object.prototype.toString.call(e)==="[object String]"}function p(e){"use strict";var t=[];(function n(e,t){if(e&&e.length>0){var u=e.shift();if(i.test(u)){n(e,t)}else if(r.test(u)){t.push(u),n(e,t)}else if(s.test(u)&&e.length>=3){var f=e.shift(),l=e.shift(),c=e.shift();if(r.test(f)&&a.test(l)&&r.test(c)){t.push({name:f,value:c}),n(e,t)}}else if(o.test(u)){n(e,t)}else{throw"Unknown token or bad expression."}}})(e.match(f),t);return t}function d(e,t,n){var r=null;(function i(e,t,n){if(e){var s=t.shift();if(s){if(t.length>0){if(c(s)){}else i(e[s],t,n)}else{if(n!==undefined){e[s]=n}r=s in e?e[s]:null}}}else{r=null}})(e,t.slice(0),n);return r}function v(e,t){for(var n in t){if(!self.isObject(t[n])){e[n]=t[n]}else{e[n]=n in e?e[n]:(e[n]={},e[n]);v(e[n],t[n])}}}function m(){return{leafy:e,root:t,stack:n,parse:p,resolve:d,merge:v,isString:h,isObject:c}}var e=function(t,n){return new e.fn.init(t,n)};var t={},n=[];var r=/([a-zA-Z0-9$_]+)|(^)/i,i=/\./,s=/\[/,o=/('])|("])/,u=/('|")/,a=/(=")|(=')/,f=/([a-zA-Z0-9$_']+)|(=")|("])|(=')|(=)|(\[)|(.)|(\^)/g,l=/\^/i;e.fn=e.prototype={constructor:e,init:function(e,r){n=h(r)?p(r):r||[];t=e||{}},"with":function(r){var i=p(r);for(var s in i){l.test(i[s])?n.pop():n.push(i[s])}return new e.fn.init(t,n)},val:function(e){return d(t,n)||e},set:function(r){d(t,n,r);return new e.fn.init(t,n)},root:function(){return t},attach:function(r,i){e.fn[r]=e.prototype[r]=function(){var r=Array.prototype.slice.call(arguments,0);i.apply(m(),r);return new e.fn.init(t,n)}}};e.fn.init.prototype=e.fn;return e}();return e=t}(typeof window==="undefined"?module.exports:window);
   
    _.mixin({maybe:function(e,t,n){function p(e){return Object.prototype.toString.call(e)==="[object Object]"}function d(e){return Object.prototype.toString.call(e)==="[object String]"}function v(e){"use strict";var t=[];(function n(e,t){if(e&&e.length>0){var r=e.shift();if(o.test(r)){n(e,t)}else if(s.test(r)){t.push(r),n(e,t)}else if(u.test(r)&&e.length>=3){var i=e.shift(),f=e.shift(),c=e.shift();if(s.test(i)&&l.test(f)&&s.test(c)){t.push({name:i,value:c}),n(e,t)}}else if(a.test(r)){n(e,t)}else{throw"Unknown token or bad expression."}}})(e.match(c),t);return t}function m(e,t,n){var r=null;(function i(e,t,n){if(e){var s=t.shift();if(s){if(t.length>0){if(p(s)){}else i(e[s],t,n)}else{if(n!==undefined){e[s]=n}r=s in e?e[s]:null}}}else{r=null}})(e,t.slice(0),n);return r}var r={},i=[];var s=/([a-zA-Z0-9$_]+)|(^)/i,o=/\./,u=/\[/,a=/('])|("])/,f=/('|")/,l=/(=")|(=')/,c=/([a-zA-Z0-9$_']+)|(=")|("])|(=')|(=)|(\[)|(.)|(\^)/g,h=/\^/i;i=d(t)?v(t):t||[];r=e||{};return m(r,i)||n}});
   
    var people = [{ name : { first: "foo" } }, { name : { } }, { }];
};
</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
leafy
for (var i = 0; i < 10000; i++) {
    _.each(people, function (p){
        leafy(p, 'name.first').val('n/a');
    });
}
pending…
mixin
for (var i = 0; i < 10000; i++) {
    _.each(people, function (person) {
        _.maybe(person, 'name.first', 'n/a');
    });
}
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

Add a comment