BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Es-Toolkit, a Modern Lodash Alternative

Es-Toolkit, a Modern Lodash Alternative

Es-toolkit is a modern JavaScript utility library that offers a preformant, type-safe alternative to Lodash.

Historically, JavaScript struggled to keep up with the needs and expectations of the developer community. This gap led to the rise of libraries such as Lodash, jQuery, and MomentJS, which helped fill this gap by providing an extensive set of useful functions missing from the language.

While JavaScript's capabilities have expanded over the years, and sites like youmightnotneed.com demonstrate how to avoid using third-party utility libraries when developing modern JavaScript applications, many developers continue to use them as they can offer improved performance, ongoing updates, and, in some cases, simply a familiar interface.

es-toolkit is a new contender in this space. It directly competes against Lodash and Underscore.js and claims impressive improvements in bundle size and performance.

Bundle Footprint Comparison

From a bundle size perspective, es-toolkit shows some impressive differences that seem to be attributed to Lodash functions being interdependent, limiting the gains of importing single methods.

  es-toolkit@0.0.1 lodash-es@4.17.21 Difference
sample 88 bytes 2000 bytes -95.6%
difference 91 bytes 3190 bytes -97.2%
sum 152 bytes 413 bytes -63.2%
debounce 144 bytes 1400 bytes -89.7%
throttle 110 bytes 1460 bytes -92.5%
pick 657 bytes 3860 bytes -83.0%
zip 797 bytes 1790 bytes -55.5%

Performance Comparison

The performance gains are even more impressive and can have real effects on JavaScript applications that handle large data sets.

  es-toolkit@0.0.1 lodash-es@4.17.21 Difference
omit 4,767,360 times 403,624 times 11.8×
pick 9,121,839 times 2,663,072 times 3.43×
differenceWith 9,291,897 times 4,275,222 times 2.17×
difference 10,436,101 times 5,155,631 times 2.02×
intersectionWith 8,074,722 times 3,814,479 times 2.12×
intersection 9,999,571 times 4,630,316 times 2.15×
unionBy 6,435,983 times 3,794,899 times 1.69×
union 5,059,209 times 4,771,400 times 1.06×
dropRightWhile 7,529,559 times 5,606,439 times 1.34×
groupBy 5,000,235 times 5,206,286 times 0.96×

es-toolkit provides an added compatibility layer for Lodash to help developers who wish to make the transition.

To make use of the compatibility layer, modify the imports from the individual capability module to the compatibility module, i.e. instead of importing `chunk` from 'es-toolkit/array,' import it from 'es-toolkit/compat'.

The compatibility layer can have a slight impact on size and performance. Before using it, it's worth comparing the documentation of both es-toolkit Lodash, as many of the simpler functions, such as chunk, uniq, etc., can be replaced directly.

es-toolkit was developed by Toss and released under the MIT license. It has been slightly modified to accommodate the use of Lodash tests (which ensure compatibility is maintained).

To start using es-toolkit, head over to their repository or install it directly from the CLI using `npm i es-toolkit'.

About the Author

Rate this Article

Adoption
Style

BT