天天看點

lodash和下劃線之間的差異[關閉]

本文翻譯自:Differences between lodash and underscore [closed]

Why would someone prefer either the lodash.js or underscore.js utility library over the other?

為什麼有人更喜歡lodash.js或underscore.js實用程式庫而不是另一個?

Lodash seems to be a drop-in replacement for underscore, the latter having been around longer.

Lodash似乎是下劃線的替代品,後者已經存在了更長時間。

I think both are brilliant, but I do not know enough about how they work to make an educated comparison, and I would like to know more about the differences.

我認為兩者都很精彩,但我對他們如何努力進行有根據的比較并不了解,我想更多地了解這些差異。

#1樓

參考:https://stackoom.com/question/vrJC/lodash和下劃線之間的差異-關閉

#2樓

Lo-Dash is inspired by underscore, but nowadays is superior solution.

Lo-Dash的靈感來自下劃線,但現在是卓越的解決方案。

You can make your custom builds , have a higher performance , support AMD and have great extra features .

您可以制作自定義版本 ,具有更高性能 ,支援AMD并具有強大的額外功能 。

Check this Lo-Dash vs Underscore benchmarks on jsperf and.. this awesome post about lo-dash :

檢查這個關于jsperf的Lo-Dash vs Underscore 基準測試和... 關于lo-dash的這篇很棒的文章 :

One of the most useful feature when you work with collections, is the shorthand syntax:

使用集合時最有用的功能之一是簡寫文法:
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 }]
           

(taken from lodash docs )

(摘自lodash docs )

#3樓

I created Lo-Dash to provide more consistent cross-environment iteration support for arrays, strings, objects, and

arguments

objects 1 .

我建立了Lo-Dash,為數組,字元串,對象和

arguments

對象1提供更一緻的跨環境疊代支援。

It has since become a superset of Underscore, providing more consistent API behavior, more features (like AMD support, deep clone, and deep merge), more thorough documentation and unit tests (tests which run in Node, Ringo, Rhino, Narwhal, PhantomJS, and browsers), better overall performance and optimizations for large arrays/object iteration, and more flexibility with custom builds and template pre-compilation utilities.

它已成為Underscore的超集,提供更一緻的API行為,更多功能 (如AMD支援,深度克隆和深度合并),更全面的文檔和單元測試(在Node,Ringo,Rhino,Narwhal,PhantomJS中運作的測試)和浏覽器),更好的整體性能和大型數組/對象疊代的優化,以及自定義建構和模闆預編譯實用程式的更大靈活性。

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.

由于Lo-Dash比

lodash underscore

更新頻繁, 是以提供了

lodash underscore

建構,以確定與最新穩定版本的

lodash underscore

相容。

At one point I was even given push access to Underscore, in part because Lo-Dash is responsible for raising more than 30 issues;

有一次,我甚至獲得了對Underscore的推動 ,部分原因是Lo-Dash負責籌集超過30個問題;

landing bug fixes, new features, & perf gains in Underscore v1.4.x+.

Underscore v1.4.x +中的登陸錯誤修複,新功能和性能提升。

In addition there are at least 3 Backbone boilerplates that include Lo-Dash by default and Lo-Dash is now mentioned in Backbone's official documentation .

此外,預設情況下至少有3個Backbone樣闆包含Lo-Dash,現在Backbone的官方文檔中提到了Lo-Dash。

Check out Kit Cambridge's post, Say "Hello" to Lo-Dash , for a deeper breakdown on the differences between Lo-Dash and Underscore.

檢視Kit Cambridge的文章, 向Lo-Dash說“你好” ,以便更深入地分析Lo-Dash和Underscore之間的差異。

Footnotes:

腳注:
  1. Underscore has inconsistent support for arrays, strings, objects, and

    arguments

    objects. Underscore對數組,字元串,對象和

    arguments

    對象的支援不一緻。
    In newer browsers, Underscore methods ignore holes in arrays , "Objects" methods iterate

    arguments

    objects, strings are treated as array-like, and methods correctly iterate functions (ignoring their "prototype" property) and objects (iterating shadowed properties like "toString" and "valueOf"), while in older browsers they will not. 在較新的浏覽器中,Underscore方法忽略數組中的漏洞 ,“Objects”方法疊代

    arguments

    對象,字元串被視為類似數組,方法正确疊代函數(忽略它們的“原型”屬性)和對象(疊代陰影屬性,如“toString”)和“valueOf”),而在較舊的浏覽器中他們不會。
    Also, Underscore methods like

    _.clone

    preserve holes in arrays, while others like

    _.flatten

    don't. 此外,像

    _.clone

    這樣的

    _.clone

    方法

    _.clone

    保留數組中的漏洞,而像

    _.flatten

    這樣的其他

    _.flatten

    則不會。

#4樓

In addition to John's answer, and reading up on lodash (which I had hitherto regarded as a "me-too" to underscore), and seeing the performance tests, reading the source-code, and blog posts , the few points which make lodash much superior to underscore are these:

除了John的回答,還讀起lodash(我迄今為止被認為是“me-too”以強調),并看到性能測試,閱讀源代碼和部落格文章 ,使得lodash的幾個要點這些更優于下劃線:
  1. It's not about the speed, as it is about consistency of speed (?) 這不是關于速度,而是關于速度的一緻性 (?)
    If you look into underscore's source-code, you'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. Firefox的某些功能非常快,而且在一些Chrome中占主導地位。 (I imagine there would be some scenarios where IE would dominate too). (我想有一些情況下IE也會占據主導地位)。 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! 我現在驚呆了,看到一個lodash比簡單的 原生功能(如Chrome中的

    Array.every

    快了100-150%!
  2. The extras in lodash are also quite useful. lodash中的附加功能也非常有用。
  3. As for Xananax's highly upvoted comment suggesting contribution to underscore's code: It's always better to have GOOD competition, not only does it keep innovation going, but also drives you to keep yourself (or your library) in good shape. 至于Xananax的高評價意見表明對下劃線代碼的貢獻:擁有良好的競争總是更好,它不僅能保持創新,還能促使你保持自己(或你的圖書館)的良好狀态。

Here is a list of differences between lodash, and it's underscore-build is a drop-in replacement for your underscore projects.

以下是lodash之間的差異清單 ,它的下劃線建構是您的下劃線項目的直接替代品。

#5樓

This is 2014 and a couple of years too late.

這是2014年,而且已經太晚了幾年。

Still I think my point holds:

我仍然認為我的觀點是:

IMHO this discussion got blown out of proportion quite a bit.

恕我直言,這次讨論相當不成比例。

Quoting the aforementioned blog post :

引用上述博文 :
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. 大多數JavaScript實用程式庫(例如Underscore,Valentine和wu)都依賴于“本機優先的雙重方法”。這種方法更喜歡本機實作,隻有在不支援本機等效時才會回歸到vanilla JavaScript。 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. 但jsPerf揭示了一個有趣的趨勢:疊代數組或類似數組的最有效方法是完全避免本機實作,而選擇簡單的循環。

As if "simple loops" and "vanilla Javascript" are more native than Array or Object method implementations.

好像“簡單循環”和“vanilla Javascript”比Array或Object方法實作更本地化。

Jeez ...

Jeez ......

It certainly would be nice to have a single source of truth, but there isn't.

擁有單一的事實來源肯定會很好,但事實并非如此。

Even if you've been told otherwise, there is no Vanilla God, my dear.

即使你已被告知,親愛的,也沒有香草神。

I'm sorry.

對不起。

The only assumption that really holds is that we are all writing Javascript code that aims at performing well in all major browsers, knowing that all of them have different implementations of the same things.

唯一真正成立的假設是我們都在編寫旨在在所有主流浏覽器中都表現良好的Javascript代碼,因為他們都知道所有這些代碼都有不同的實作。

It's a bitch to cope with, to put it mildly.

溫和地說,這是一個應對的婊子。

But that's the premise, whether you like it or not.

但這是前提,無論你喜不喜歡。

Maybe y'all are working on large scale projects that need twitterish performance so that you really see the difference between 850,000 (underscore) vs. 2,500,000 (lodash) iterations over a list per sec right now!

也許你們都在開展大規模的項目,這些項目需要慌張的表現才能讓你真正看到每秒在清單上的850,000(下劃線)和2,500,000(lodash)疊代之間的差異!

I for one am not.

我不是一個人。

I mean, I worked projects where I had to address performance issues, but they were never solved or caused by neither Underscore nor Lo-Dash.

我的意思是,我工作的項目我必須解決性能問題,但它們從未解決或由Underscore和Lo-Dash引起。

And unless I get hold of the real differences in implementation and performance (we're talking C++ right now) of lets say a loop over an iterable (object or array, sparse or not!), I rather don't get bothered with any claims based on the results of a benchmark platform that is already opinionated .

除非我抓住實作和性能的真正差異(我們現在正在談論C ++),讓我們說一個循環遍及一個可疊代的(對象或數組,稀疏或不稀疏!),我甯願不被任何困擾基于已經輿論的基準平台的結果提出索賠。

It only needs one single update of lets say Rhino to set its Array method implementations on fire in a fashion that not a single "medieval loop methods perform better and forever and whatnot" priest can argue his/her way around the simple fact that all of a sudden array methods in FF are much faster than his/her opinionated brainfuck.

它隻需要一個單獨的更新讓我們說Rhino以一種方式設定它的Array方法實作,這種方式沒有一個“中世紀循環方法表現更好,永遠而且什麼都不是”牧師可以圍繞這個簡單事實争論他/她的所有方式FF中的突然陣列方法比他/她認為的腦力快得多。

Man, you just can't cheat your runtime environment by cheating your runtime environment!

夥計,你不能通過欺騙你的運作時環境來欺騙你的運作時環境!

Think about that when promoting ...

在推廣時考慮一下......
your utility belt 你的實用腰帶

... next time.

... 下次。

So to keep it relevant:

是以要保持相關性:
  • Use Underscore if you're into convenience without sacrificing native ish. 如果您在不犧牲原生能力的情況下進入友善,請使用Underscore。
  • Use Lo-Dash if you're into convenience and like its extended feature catalogue (deep copy etc.) and if you're in desperate need of instant performance and most importantly don't mind settling for an alternative as soon as native API's outshine opinionated workaurounds. 使用Lo-Dash,如果你是友善的,并且喜歡它的擴充功能目錄(深層拷貝等),如果你迫切需要即時性能,最重要的是不要介意在本機API出現時立即尋求替代方案固執己見的工作。 Which is going to happen soon. 哪個會很快發生。 Period. 期。
  • There's even a third solution. 甚至還有第三種解決方案。 DIY! DIY! Know your environments. 了解您的環境。 Know about inconsistencies. 了解不一緻。 Read their ( John-David 's and Jeremy 's) code. 閱讀他們( 約翰 - 大衛和傑裡米的)代碼。 Don't use this or that without being able to explain why a consistency/compatibility layer is really needed and enhances your workflow or improves the performance of your app. 如果不能解釋為什麼真正需要一緻性/相容性層并且增強您的工作流程或提高應用程式的性能,請不要使用這個或那個。 It is very likely that your requirements are satisfied with a simple polyfill that you're perfectly able to write yourself. 很可能您的要求滿足于您完全能夠自己編寫的簡單的填充物。 Both libraries are just plain vanilla with a little bit of sugar. 這兩個圖書館都是普通的香草,帶有一點點糖。 They both just fight over who's serving the sweetest pie . 他們倆隻是為誰最甜蜜的餡餅而戰 。 But believe me, in the end both are only cooking with water. 但相信我,最後兩人都隻是用水做飯。 There's no Vanilla God so there can't be no Vanilla pope, right? 沒有香草神,是以不可能沒有香草教皇,對嗎?

Choose whatever approach fits your needs the most.

選擇最适合您需求的方法。

As usual.

照常。

I'd prefer fallbacks on actual implementations over opinionated runtime cheats anytime but even that seems to be a matter of taste nowadays.

我更喜歡在實際的實作上回落而不是固執的運作時作弊,但即便如此,這似乎也是現在的品味問題。

Stick to quality resources like http://developer.mozilla.com and http://caniuse.com and you'll be just fine.

堅持像http://developer.mozilla.com和http://caniuse.com這樣的優質資源,你會很好。

#6樓

http://benmccormick.org/2014/11/12/underscore-vs-lodash/

http://benmccormick.org/2014/11/12/underscore-vs-lodash/

Latest article comparing the two by Ben McCormick:

Ben McCormick将兩者進行比較的最新文章:
  1. Lo-Dash's API is a superset of Underscore's. Lo-Dash的API是Underscore的超集。
  2. Under the hood [Lo-Dash] has been completely rewritten. 引擎蓋下[Lo-Dash]已被完全重寫。
  3. Lo-Dash is definitely not slower than Underscore. Lo-Dash肯定不比Underscore慢。
  4. What has Lo-Dash added? Lo-Dash添加了什麼?
    • Usability Improvements 可用性改進
    • Extra Functionality 額外功能
    • Performance Gains 業績增長
    • Shorthand syntaxes for chaining 連結的簡寫文法
    • Custom Builds to only use what you need 自定義建構僅使用您需要的内容
    • Semantic versioning and 100% code coverage 語義版本控制和100%代碼覆寫率

繼續閱讀