Convert Figma logo to code with AI

jashkenas logounderscore

JavaScript's utility _ belt

27,294
5,530
27,294
51

Top Related Projects

59,602

A modern JavaScript utility library delivering modularity, performance, & extras.

23,757

:ram: Practical functional Javascript

List of JavaScript methods which you can use natively + ESLint Plugin

💎  Convenient and dependency free wrapper for working with arrays and objects

1,290

Modular JavaScript Utilities

Quick Overview

Underscore is a popular JavaScript utility library that provides a collection of functional programming helpers without extending any built-in objects. It offers over 100 functions that support both the usual functional suspects (map, filter, invoke) as well as more specialized helpers (function binding, JavaScript templating, deep equality testing, and more).

Pros

  • Lightweight and has no dependencies
  • Provides a consistent API across different JavaScript environments
  • Enhances productivity by offering a wide range of utility functions
  • Well-documented and has a large community

Cons

  • Some functionalities are now available natively in modern JavaScript
  • Performance can be slower compared to native methods in some cases
  • Learning curve for developers new to functional programming concepts
  • Adds an extra library to your project, increasing bundle size

Code Examples

  1. Using _.map to transform an array:
const numbers = [1, 2, 3, 4, 5];
const doubled = _.map(numbers, num => num * 2);
console.log(doubled); // [2, 4, 6, 8, 10]
  1. Filtering an array with _.filter:
const mixed = [1, 2, 'three', 4, 'five', 6];
const onlyNumbers = _.filter(mixed, item => typeof item === 'number');
console.log(onlyNumbers); // [1, 2, 4, 6]
  1. Using _.template for simple templating:
const compiled = _.template("Hello <%= name %>!");
console.log(compiled({ name: 'John' })); // "Hello John!"
  1. Chaining operations with _.chain:
const result = _.chain([1, 2, 3, 4, 5, 6])
  .filter(num => num % 2 === 0)
  .map(num => num * 3)
  .value();
console.log(result); // [6, 12, 18]

Getting Started

To use Underscore in your project, you can include it via CDN or install it using npm:

npm install underscore

Then, in your JavaScript file:

// Using ES6 modules
import _ from 'underscore';

// Or using CommonJS
const _ = require('underscore');

// Now you can use Underscore functions
const numbers = [1, 2, 3, 4, 5];
const sum = _.reduce(numbers, (memo, num) => memo + num, 0);
console.log(sum); // 15

Competitor Comparisons

59,602

A modern JavaScript utility library delivering modularity, performance, & extras.

Pros of Lodash

  • Better performance for many operations
  • More comprehensive set of utility functions
  • Modular structure allowing for cherry-picking specific functions

Cons of Lodash

  • Larger file size when using the full library
  • Steeper learning curve due to more extensive API

Code Comparison

Underscore:

_.map([1, 2, 3], function(num) { return num * 3; });
_.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });

Lodash:

_.map([1, 2, 3], num => num * 3);
_.filter([1, 2, 3, 4, 5, 6], num => num % 2 === 0);

Key Differences

  • Lodash offers more consistent cross-browser behavior
  • Underscore has a smaller footprint and simpler API
  • Lodash provides additional features like deep cloning and more advanced array/object manipulation
  • Underscore focuses on functional programming paradigms
  • Lodash has better support for working with large datasets

Community and Maintenance

  • Both libraries have active communities
  • Lodash tends to receive more frequent updates and has a larger contributor base
  • Underscore has a more stable API with fewer breaking changes

Use Cases

  • Underscore is suitable for smaller projects or when a lightweight solution is needed
  • Lodash is preferred for larger applications or when advanced utility functions are required
23,757

:ram: Practical functional Javascript

Pros of Ramda

  • Emphasizes functional programming paradigms with immutable data structures
  • Supports function composition and currying out of the box
  • Designed for better tree-shaking, allowing for smaller bundle sizes

Cons of Ramda

  • Steeper learning curve for developers not familiar with functional programming
  • Less widespread adoption compared to Underscore
  • May require additional setup or configuration in some projects

Code Comparison

Underscore:

_.map([1, 2, 3], function(num) { return num * 2; });
_.filter([1, 2, 3, 4], function(num) { return num % 2 == 0; });

Ramda:

R.map(x => x * 2, [1, 2, 3]);
R.filter(x => x % 2 === 0, [1, 2, 3, 4]);

Both libraries provide utility functions for working with arrays and objects, but Ramda's approach is more aligned with functional programming principles. Ramda's functions are automatically curried, allowing for easier function composition and partial application. Underscore, on the other hand, offers a more imperative style that may be more familiar to developers coming from object-oriented backgrounds.

While Underscore has been a popular choice for many years, Ramda has gained traction among developers who prefer a more functional approach to JavaScript programming. The choice between the two often depends on the project requirements and the team's familiarity with functional programming concepts.

List of JavaScript methods which you can use natively + ESLint Plugin

Pros of You-Dont-Need-Lodash-Underscore

  • Promotes native JavaScript methods, reducing dependency on external libraries
  • Provides modern alternatives that can lead to better performance and smaller bundle sizes
  • Serves as an educational resource for learning native JavaScript capabilities

Cons of You-Dont-Need-Lodash-Underscore

  • Lacks the comprehensive API and consistent cross-browser support of Underscore
  • May require more verbose code in some cases compared to Underscore's concise methods
  • Doesn't provide a unified interface for handling different browser implementations

Code Comparison

Underscore:

_.map([1, 2, 3], function(num) { return num * 2; });

You-Dont-Need-Lodash-Underscore (native JavaScript):

[1, 2, 3].map(num => num * 2);

Underscore:

_.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });

You-Dont-Need-Lodash-Underscore (native JavaScript):

[1, 2, 3, 4, 5, 6].filter(num => num % 2 === 0);

Both approaches achieve similar results, but You-Dont-Need-Lodash-Underscore encourages using native JavaScript methods, which can lead to more maintainable and future-proof code. However, Underscore provides a consistent API across browsers and includes additional utility functions that may be useful in certain scenarios.

💎  Convenient and dependency free wrapper for working with arrays and objects

Pros of collect.js

  • More modern syntax and ES6+ features
  • Chainable methods for improved readability
  • Extensive documentation with examples

Cons of collect.js

  • Smaller community and fewer contributors
  • Less battle-tested in production environments
  • Fewer utility functions compared to Underscore

Code Comparison

collect.js:

collect([1, 2, 3, 4])
  .filter(n => n > 2)
  .map(n => n * 2)
  .toArray();

Underscore:

_.chain([1, 2, 3, 4])
  .filter(n => n > 2)
  .map(n => n * 2)
  .value();

Summary

collect.js is a more modern alternative to Underscore, offering chainable methods and improved syntax for working with collections in JavaScript. It provides extensive documentation and examples, making it easier for developers to get started. However, Underscore has a larger community, more contributors, and a longer track record in production environments. Underscore also offers a wider range of utility functions beyond collection manipulation.

The code comparison shows that both libraries support method chaining, but collect.js uses a more intuitive toArray() method to finalize the chain, while Underscore uses value(). collect.js's syntax feels more natural for developers familiar with modern JavaScript practices.

Choose collect.js for modern projects prioritizing readability and chainable operations, while Underscore remains a solid choice for projects requiring a broader set of utility functions and a more established ecosystem.

1,290

Modular JavaScript Utilities

Pros of mout

  • Modular architecture allows for selective importing, potentially reducing bundle size
  • Broader utility function coverage, including more advanced array and object manipulation
  • AMD, CommonJS, and global support for better compatibility across different environments

Cons of mout

  • Less popular and less widely adopted compared to Underscore
  • Documentation may not be as comprehensive or user-friendly
  • Smaller community and ecosystem around the project

Code Comparison

mout:

import map from 'mout/array/map';
import sum from 'mout/math/sum';

const numbers = [1, 2, 3, 4];
const doubled = map(numbers, n => n * 2);
const total = sum(doubled);

Underscore:

const _ = require('underscore');

const numbers = [1, 2, 3, 4];
const doubled = _.map(numbers, n => n * 2);
const total = _.reduce(doubled, (sum, n) => sum + n, 0);

Summary

mout offers a more modular approach with a wider range of utility functions, making it suitable for projects that require specific functionality without unnecessary bloat. However, Underscore benefits from greater popularity, more extensive documentation, and a larger community. The choice between the two depends on project requirements, bundle size considerations, and personal preference for API style and ecosystem support.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

                   __
                  /\ \                                                         __
 __  __    ___    \_\ \     __   _ __   ____    ___    ___   _ __    __       /\_\    ____
/\ \/\ \ /' _ `\  /'_  \  /'__`\/\  __\/ ,__\  / ___\ / __`\/\  __\/'__`\     \/\ \  /',__\
\ \ \_\ \/\ \/\ \/\ \ \ \/\  __/\ \ \//\__, `\/\ \__//\ \ \ \ \ \//\  __/  __  \ \ \/\__, `\
 \ \____/\ \_\ \_\ \___,_\ \____\\ \_\\/\____/\ \____\ \____/\ \_\\ \____\/\_\ _\ \ \/\____/
  \/___/  \/_/\/_/\/__,_ /\/____/ \/_/ \/___/  \/____/\/___/  \/_/ \/____/\/_//\ \_\ \/___/
                                                                              \ \____/
                                                                               \/___/

Underscore.js is a utility-belt library for JavaScript that provides support for the usual functional suspects (each, map, reduce, filter...) without extending any core JavaScript objects.

For Docs, License, Tests, and pre-packed downloads, see: https://underscorejs.org

For support and questions, please consult our security policy, the gitter channel or stackoverflow

Underscore is an open-sourced component of DocumentCloud: https://github.com/documentcloud

Many thanks to our contributors: https://github.com/jashkenas/underscore/contributors

You can support the project by donating on Patreon. Enterprise coverage is available as part of the Tidelift Subscription.

This project adheres to a code of conduct. By participating, you are expected to uphold this code.

NPM DownloadsLast 30 Days