javascript tutorial - [Solved-4 Solutions] Differences between lodash and underscore - javascript - java script - javascript array



Problem:

Why would most of them prefer either the lodash.js or underscore.js utility library over the other?

Solution 1:

Because Lo-Dash is updated more frequently than Underscore, a lodash underscore build is provided to ensure compatibility with the latest stable version of Underscore.

Solution 2:

var characters = [
  { 'name': 'barney', 'age': 36, 'blocked': false },
  { 'name': 'fred',   'age': 40, 'blocked': true }
];

// using "_.filter" callback shorthand
_.filter(characters, { 'age': 36 });

// using underscore
_.filter(characters, function(character) { return character.age === 36; } );

// → [{ 'name': 'barney', 'age': 36, 'blocked': false }]


click below button to copy the code. By JavaScript tutorial team

Solution 3:

Most JavaScript utility libraries, such as Underscore, Valentine, and wu, rely on the “native-first dual approach.” This approach prefers native implementations, falling back to vanilla JavaScript only if the native equivalent is not supported. But jsPerf revealed an interesting trend: the most efficient way to iterate over an array or array-like collection is to avoid the native implementations entirely, opting for simple loops instead.

Solution 4:

If we look into underscore's source-code, we'll see in the first few lines that underscore falls-back on the native implementations of many functions. Although in an ideal world, this would have been a better approach, if you look at some of the perf links given in these slides , it is not hard to draw the conclusion that the quality of those 'native implementations' vary a lot browser-to-browser. Firefox is damn fast in some of the functions, and in some Chrome dominates. (I imagine there would be some scenarios where IE would dominate too). I believe that it's better to prefer a code whose performance is more consistent across browsers. Do read the blog post earlier, and instead of believing it for its sake, judge for yourself by running the benchmarks. I am stunned right now, seeing a lodash performing 100-150% faster than underscore in even simple, native functions such as Array.every in Chrome!


Related Searches to javascript tutorial - Differences between lodash and underscore