This content originally appeared on Bits and Pieces - Medium and was authored by Poorna Theekshana
Why Radash is Better than Loadash?
Lodash and Radash are JavaScript libraries that offer valuable functions for typical programming tasks. Lodash has been there for a significant time, whereas Radash is the new kid on the block.
Usage of these libraries has significantly increased in the past few years since they simplify many common programming tasks. For example, Lodash has more than 45 million weekly downloads in NPM and 55.4K+ GitHub stars. However, Lodash has not received any significant updates for 2 years, while new libraries like Radash provide constant updates to resolve modern programming issues.
In this article, I will discuss the issues in Lodash and how radash solves them in detail to answer the ultimate question: Will Radash Replace Lodash?
What is Lodash?
Lodash is a well-known JS utility library with more than 100 JavaScript functions like map, filter, javascript templating, deep equality checks, and more. You can directly import and use these functions in your application to simplify common programming tasks like:
- Iterating arrays, objects, & strings.
- Manipulating & testing values.
- Creating composite functions.
The code snippet below shows how to replace a traditional for loop with Lodash.
var myArray = [1,2,3,4,5,6,7,8,9]
// Without Lodash
for (i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
// With Lodash
_.each(myArray, function (value, key) {
console.log(value);
});
As you can see, with Lodash, developers do not need to define variables, check conditions, or increment values to write a for loop.
You can easily install Lodash using NPM.
npm i --save lodash
The downfall of Lodash
Initially, Lodash had a great run and helped developers to write clean, maintainable JavaScript codes. However, it hasn’t received a major update for the latest JavaScript functions, and developers have started to face some challenges when using Lodash.
Numerous Lodash functions become obsolete with the introduction of optional chaining and null coalescing. The _.filter function of Lodash is a great example of that. It was a great option to iterate through object arrays and filter out records based on attributes without errors, even if the attribute does not exist on the object.
import _ from "lodash";
var users = [
{ user: "Poorna", age: 26, active: true },
{ user: "Widura", age: 28 },
{ user: "Binara", age: 24, active: true }
];
let filtered_users = _.filter(users, { active: true });
console.log(filtered_users);
// { 'user': 'Poorna', 'age': 26, 'active': true }
// { 'user': 'Binara', 'age': 24, 'active': true }
But now, we can use the optional chaining operator for that. It is much simpler and does not require importing any third-party libraries.
var users = [
{ user: "Poorna", age: 26, active: true },
{ user: "Widura", age: 28 },
{ user: "Binara", age: 24, active: true }
];
let filtered_users = users.filter(user => user?.active == true );
console.log(filtered_users);
// { 'user': 'Poorna', 'age': 26, 'active': true }
// { 'user': 'Binara', 'age': 24, 'active': true }
Similarly, functions like _.get, _.map and _.size have also become obsolete due to the latest JavaScript and TypeScript features.
In terms of performance, features like optional chaining were way ahead of Lodash functions. According to measurethat.net, optional chaining can perform almost 2 times faster than Lodash. _.get function.
Moreover, developers often complain that it’s difficult and time-consuming to understand exactly how Lodash functions work since it does not provide clear API documentation for developers. However, we must admit that Lodash still has a strong user base with more than 49 million weekly NPM downloads.
What is Radash?
Source: https://www.npmjs.com/package/radash
Radash is one of the latest zero-dependency JavaScript utility libraries that is becoming popular among developers. It provides exciting new features to replace the obsolete Lodash functions and constantly releases updates to keep in sync with the latest JavaScript features. Also, it is written with TypeScript, and types come prepackaged.
Although Radash is new, it has 1.9K+ GitHub stars, 66 Forks, and 11.5K+ weekly NPM downloads. You can easily install Radash using NPM or Yarn.
// NPM
npm install radash
// Yarn
yarn add radash
The rise of Radash
We can use Radash in place of the Lodash library for several reasons. Radash aims to provide robust functions to resolve modern issues in JavaScript. For example, it strongly supports TypeScript features compared to Lodash.
Also, the functions in Radash are well-typed, well-tested, well-documented, and written with simplicity as the top priority. Most importantly, these functions resolve issues in modern JavaScript.
The following functions will give you some ideas on why you should use Radash.
range() function
The [range()](https://radash-docs.vercel.app/docs/array/range) function is a great feature of Radash since it can replace traditional loops. For example, assume that you need to print from 1 to 5. If you use a traditional for loop, it will look like the below:
for (let i = 1; i <= 5; i++){
console.log(i);
}
But, with the Radash range() function, you can omit all the variable declarations, conditions and increments.
import { range } from 'radash';
for (const i of range(1,5)){
console.log(i);
}
As you can see, it is simple, and anyone can easily understand what’s happening in the loop. It accepts a minimum of 1 and a maximum of 4 input parameters. Based on the number of arguments you provide, the behaviours of the range() function will change.
For example:
- range(size) — range(5) // 0, 1, 2, 3, 4, 5
- range(start, end) — range(2,5) // 2, 3, 4, 5
- range(start, end, value) — The value can be a function. range(2, 5, (i) => i) // 2, 3, 4, 5
- range(start, end, value, step) — (2, 5, i => i, 2) // 2, 4
list() function
The [list()](https://radash-docs.vercel.app/docs/array/list) function is also pretty similar to the range() function. You can use it to generate lists with specific items dynamically. It also accepts a minimum of 1 and a maximum of 4 input parameters like the range() function.
import { list } from 'radash'
var my_list = list(25, 100, i => i, 25)
console.log(my_list) // // [25, 50, 75, 100]
retry() function
Radash’s [retry()](https://radash-docs.vercel.app/docs/async/retry) function allows you to retry a failed asynchronous operation. The function takes in an operation, a retry count, and a delay. It will continuously re-attempt the operation until it succeeds or reaches the maximum retry count.
import { retry } from 'radash'
await retry({ times: 2, delay: 1000 }, api.articles.list)
Most developers use this function as a replacement for the async-retry library. You can replace most existing fault tolerance libraries for backend services by combining Radash tryit, parallel, and retry.
counting() function
The [counting()](https://radash-docs.vercel.app/docs/array/counting) function allows developers to count the number of elements in an array-like collection. It accepts an array of objects and a callback function to decide the counting condition.
If you use a traditional loop, you will have to work with multiple if else conditions to get the final result.
const users = [
{name: 'Poorna', type: 'engineer'},
{name: 'Widura', type: 'manager'},
{name: 'Binara', type: 'engineer'},
]
engineerCount = 0;
managerCount = 0;
for (let i = 0; i < users.length; i++){
if(users[i].type == 'engineer'){
engineerCount++;
} else if (users[i].type == 'manager'){
managerCount++;
}
}
console.log(engineerCount , managerCount); // 2 1
But, if you use the counting() function, it will be just a single line:
import { counting } from 'radash'
const users = [
{name: 'Poorna', type: 'engineer'},
{name: 'Widura', type: 'manager'},
{name: 'Binara', type: 'engineer'},
]
counting(users , t => t.type) // => { engineer: 2, manager: 1 }
Typed functions
Typed functions are another great feature in Radash, It provides a set of functions like isArray(), isDate(), isFloat(), isInt() to check the type of variables.
String manipulations
Radash also provides a set of functions for string manipulations. With these functions, you can easily achieve some of the most common string formattings, like converting to camel case, converting to pascal case, trimming, etc.
Mentioned above are only some of the very useful functions of Radash. There are many more such functions. You can get them from the Radash Documentation.
Conclusion
In this article, I discussed why new developers dislike Lodash and why they are moving towards alternatives like Radash. But, compared to new competitors, Lodash still has a huge user base, and it is widely used in many large-scale projects.
However, with the latest changes in JavaScript and TypeScript, developers are more likely to go with modern utility libraries like Radash, especially for new projects.
I invite you to share your thoughts on these 2 libraries in the comments section. Thank you for reading.
Build frontend apps with independent components and teams using Micro Frontends
Bit’s open-source tool help 250,000+ devs to build apps with components.
Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.
Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:
→ Micro-Frontends
→ Design System
→ Code-Sharing and reuse
→ Monorepo
Learn more:
- How We Build Micro Frontends
- How we Build a Component Design System
- How to reuse React components across your projects
- 5 Ways to Build a React Monorepo
- How to Create a Composable React App with Bit
Will Radash Replace Lodash? was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Poorna Theekshana
Poorna Theekshana | Sciencx (2023-03-06T07:02:44+00:00) Will Radash Replace Lodash?. Retrieved from https://www.scien.cx/2023/03/06/will-radash-replace-lodash/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.