Skip to content

radash 介绍

radash 是一个现代化的 JavaScript 工具库,由前Google工程师 Ethan Dean 于2023年发起,专为 TypeScript 项目设计,提供零依赖、类型安全、高性能的实用函数集合。

核心特点

🚀 零依赖

radash 完全零依赖,不引入任何外部包,确保最小的包体积和最快的加载速度。

typescript
// 无需担心依赖冲突
import { map, filter, reduce } from 'radash'

🔒 类型安全

专为 TypeScript 设计,提供完整的类型定义和类型推断,让开发更加安全可靠。

typescript
import { isArray, isObject, isString } from 'radash'

// 完整的类型推断
const result = isArray([1, 2, 3]) // TypeScript 知道 result 是 boolean
const obj = { name: 'Alice', age: 25 }
const keys = Object.keys(obj) // 类型安全的键获取

⚡ 高性能

经过优化的算法实现,提供比原生方法更好的性能表现。

typescript
import { map, filter, reduce } from 'radash'

// 高性能的数组操作
const numbers = [1, 2, 3, 4, 5]
const doubled = map(numbers, n => n * 2)
const evens = filter(numbers, n => n % 2 === 0)
const sum = reduce(numbers, (acc, n) => acc + n, 0)

🛠️ 现代化 API

采用现代 JavaScript 特性,提供简洁直观的 API 设计。

typescript
import { debounce, throttle, memo } from 'radash'

// 现代化的函数工具
const debouncedSearch = debounce(searchFunction, 300)
const throttledScroll = throttle(scrollHandler, 100)
const memoizedCalc = memo(expensiveCalculation)

📦 模块化设计

支持按需导入,只打包你需要的函数,进一步减小包体积。

typescript
// 只导入需要的函数
import { map } from 'radash/array'
import { debounce } from 'radash/curry'
import { isString } from 'radash/typed'

与 Lodash 对比

优势对比

特性RadashLodash
包体积零依赖,体积更小有依赖,体积较大
TypeScript 支持原生支持,类型安全需要额外安装类型包
现代化使用现代 JS 特性兼容旧版本浏览器
性能优化的现代实现兼容性优先
维护状态活跃维护维护相对滞后
API 设计简洁直观功能丰富但复杂

代码对比

数组操作

typescript
// Radash - 简洁直观
import { map, filter, reduce } from 'radash'

const users = [
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
  { name: 'Charlie', age: 35 }
]

const names = map(users, user => user.name)
const adults = filter(users, user => user.age >= 30)
const totalAge = reduce(users, (sum, user) => sum + user.age, 0)

// Lodash - 功能丰富但复杂
import _ from 'lodash'

const names = _.map(users, 'name')
const adults = _.filter(users, { age: { $gte: 30 } })
const totalAge = _.sumBy(users, 'age')

类型检查

typescript
// Radash - 类型安全
import { isArray, isObject, isString } from 'radash'

if (isArray(data)) {
  // TypeScript 知道 data 是数组
  data.forEach(item => console.log(item))
}

// Lodash - 需要额外类型包
import _ from 'lodash'
import type { isArray } from 'lodash'

if (_.isArray(data)) {
  // 需要额外的类型处理
  (data as any[]).forEach(item => console.log(item))
}

函数工具

typescript
// Radash - 现代化设计
import { debounce, throttle, memo } from 'radash'

const debouncedSearch = debounce(searchFunction, 300)
const throttledScroll = throttle(scrollHandler, 100)
const memoizedCalc = memo(expensiveCalculation)

// Lodash - 传统设计
import _ from 'lodash'

const debouncedSearch = _.debounce(searchFunction, 300)
const throttledScroll = _.throttle(scrollHandler, 100)
const memoizedCalc = _.memoize(expensiveCalculation)

性能对比

typescript
// Radash - 优化的现代实现
import { map, filter } from 'radash'

const largeArray = Array.from({ length: 100000 }, (_, i) => i)

// 更快的执行速度
const doubled = map(largeArray, n => n * 2)
const evens = filter(largeArray, n => n % 2 === 0)

// Lodash - 兼容性优先
import _ from 'lodash'

const doubled = _.map(largeArray, n => n * 2)
const evens = _.filter(largeArray, n => n % 2 === 0)

Lodash和Radash依赖大小对比

通过对比可以看出,Radash 仅仅只是 Lodash 五分之一,如果 Radash 可以满足您的项目需求,还有什么理由要坚持使用 Lodash 呢?Radash 是一个全新 JS 工具库,大小只有 Lodash 的五分之一

Lodash的大小如下

Radash库压缩后大小:69.8kb,minnify+gzip后大小:24.4kb, 下载时间:3G网络: 488ms, 4G网络: 28ms
lodash size

Radash 的大小如下

Radash库压缩后大小:14.8kb,minnify+gzip后大小:4.8kb, 下载时间:3G网络: 97ms, 4G网络: 6ms
rodash size

适用场景

✅ 推荐使用 Radash 的场景

  • TypeScript 项目 - 原生类型支持
  • 现代浏览器应用 - 利用现代 JS 特性
  • 性能敏感项目 - 优化的算法实现
  • 小体积要求 - 零依赖设计
  • 新项目开发 - 现代化的 API 设计

⚠️ 考虑使用 Lodash 的场景

  • 需要兼容旧浏览器 - Lodash 提供更好的兼容性
  • 需要复杂的链式操作 - Lodash 的链式 API 更成熟
  • 需要特定的 Lodash 功能 - 某些特殊功能可能只有 Lodash 提供

快速开始

安装

bash
npm install radash
# 或
yarn add radash
# 或
pnpm add radash

基础使用

typescript
import { map, filter, isArray, debounce } from 'radash'

// 数组操作
const numbers = [1, 2, 3, 4, 5]
const doubled = map(numbers, n => n * 2)
const evens = filter(numbers, n => n % 2 === 0)

// 类型检查
if (isArray(data)) {
  console.log('这是一个数组')
}

// 函数工具
const debouncedSearch = debounce(searchFunction, 300)

按需导入

typescript
// 只导入需要的函数
import { map } from 'radash/array'
import { debounce } from 'radash/curry'
import { isString } from 'radash/typed'

生态系统

Radash 提供了丰富的功能分类:

  • Array - 数组操作工具
  • Async - 异步处理工具
  • Curry - 函数式编程工具
  • Number - 数字处理工具
  • Object - 对象操作工具
  • Random - 随机数生成工具
  • String - 字符串处理工具
  • Typed - 类型检查工具

社区支持

总结

Radash 是一个专为现代 JavaScript/TypeScript 项目设计的工具库,提供了零依赖、类型安全、高性能的实用函数集合。相比 Lodash,Radash 在类型支持、包体积、现代化程度等方面具有明显优势,特别适合新项目的开发。

选择 Radash,享受现代化的开发体验!

Released under the MIT License.