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 對比
優勢對比
特性 | Radash | Lodash |
---|---|---|
包體積 | 零依賴,體積更小 | 有依賴,體積較大 |
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 的大小如下
Lodash 庫壓縮後大小:69.8kb
,minify+gzip 後大小:24.4kb
,下載時間:3G 網絡:488ms,4G 網絡:28ms
Radash 的大小如下
Radash 庫壓縮後大小:14.8kb
,minify+gzip 後大小:4.8kb
,下載時間:3G 網絡:97ms,4G 網絡:6ms
適用場景
✅ 推薦使用 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 - 類型檢查工具
社區支持
- GitHub - https://github.com/sodiray/radash
- 文檔 - 完整的中文文檔 https://radash.uihtm.com
- 類型支持 - 完整的 TypeScript 類型定義
- 活躍維護 - 持續的功能更新和 bug 修復
總結
Radash 是一個專為現代 JavaScript/TypeScript 項目設計的工具庫,提供了零依賴、類型安全、高性能的實用函數集合。相比 Lodash,Radash 在類型支持、包體積、現代化程度等方面具有明顯優勢,特別適合新項目的開發。
選擇 Radash,享受現代化的開發體驗!