_.squeeze

JavaScript performance comparison

Test case created by Andrea

Preparation code

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var json = {
      "experience": [{
        "position": "Front-end Team Leader",
        "company": "Media Net Software",
        "period": {
          "start": "February 2012",
          "end": "Present",
          "duration": "(1 year 1 month)",
          "location": "Madrid Area, Spain"
        },
        "description": "Currently working on the new portal of BBVA bank. \r\n Writing HTML5, CSS3, SASS, modular JavaScript APIs, Backbone, underscore, jQuery &amp; more."
      }, {
        "position": "Web developer",
        "company": "Ticketea",
        "period": {
          "start": "July 2010",
          "end": "",
          "duration": "(1 year 7 months)",
          "location": "Madrid Area, Spain"
        },
        "description": "Building the product from scratch using SCRUM, TDD, LAMP, JavaScript (jQuery), XHTML, CSS, Git. Integrations with affiliates, Google and Facebook APIs."
      }, {
        "position": "Web developer",
        "company": "Comunica+A, The Internet Sales Company",
        "period": {
          "start": "May 2009",
          "end": "",
          "duration": "(1 year 2 months)",
          "location": "Madrid Area, Spain"
        },
        "description": "Websites for clients such as Orange, Endesa, MRW, McDonalds, Wilkinson, Alfa Romeo, Lancia, Real Madrid FC, entradas.com, Mutua Madrile&#xf1;a, la Caixa, red.es, AECEM, etc. LAMP, XHTML, CSS, JavaScript."
      }, {
        "position": "Web developer",
        "company": "Arionline s.r.l.",
        "period": {
          "start": "2007",
          "end": "",
          "duration": "(2 years)",
          "location": "Cagliari Area, Italy"
        },
        "description": "Software programming, R&amp;D projects, web apps, with Open Source technology."
      }],
      "languages": [{
        "language": "Italian",
        "proficiency": "(Native or bilingual proficiency)"
      }, {
        "language": "Spanish",
        "proficiency": "(Native or bilingual proficiency)"
      }, {
        "language": "English",
        "proficiency": "(Limited working proficiency)"
      }],
      "skills": ["HTML5", "CSS3", "JavaScript", "PHP", "MySQL", "jQuery", "Backbone.js", "Front-end Development", "Scrum", "Agile", "TDD", "MVC", "OOP", "Database Design", "LAMP", "PHP Frameworks", "Git", "Test Driven Development", "HTML 5", "Front-end"]
    };
   
    _.mixin({
   
      squeeze: function(obj) {
   
        var _extract = function(obj, result) {
            _.each(obj, function(propValue, propName) {
              if (propValue) {
                if (_.isArray(propValue)) {
                  _extract(propValue, result);
                } else if (_.isObject(propValue)) {
                  _extract(_.flatten(propValue), result);
                } else if (_.isString(propValue)) {
                  result.push(propValue);
                }
              }
            })
            return result;
            }
           
        return _extract(obj, []);
      },
   
        squeeze2: function (obj) {
          var extract = function (obj, result) {
            _.each(obj, function(value, name) {
             if (value && _.isString(value)) {
                result.push(value);
              } else {
                if (_.isArray(value)) {
                  extract(value, result);
                }
                if (_.isObject(value)) {
                  extract(_.flatten(value), result);
                }
              }
            })
            return result;
          }
          return extract(obj, []);
        }
    });
};
</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
_squeeze
_.squeeze(json);
pending…
_squeeze 2
_.squeeze2(json);
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