
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Small, performant & immutable iteration utilities for Arrays and Objects
A collection of small, performant & immutable iteration utilities for Arrays and Objects.
yarn add fast-loops
Alternatively use npm i --save fast-loops
.
Because JavaScript's native "functional" APIs such as forEach
, reduce
, map
and filter
are slow. There're many different utility packages out there already, e.g. lodash. But lodash's reduce
method itself is 4.5kb gzipped which is way too much for a simple Array/Object reduce utility.
All methods are immutable by defaults, yet they're not safe from mutating.
arrayEach(arr, iterator)
Iterates over each value in the array.
Similar to Array.prototype.forEach
.
arr
(Array): The array to iterate overiterator
(Function): The iterator method with the signature (value, index, length, arr) => void
import { arrayEach } from 'fast-loops'
arrayEach([1, 2, 3], console.log)
// 1, 0, 3, [1, 2, 3]
// 2, 1, 3, [1, 2, 3]
// 3, 2, 3, [1, 2, 3]
arrayFilter(arr, filter)
Filters an array according to the filter criteria.
Similar to Array.prototype.filter
.
arr
(Array): The array that gets filteredfilter
(Function): The filter method with the signature (value, index, length, arr) => boolean
import { arrayFilter } from 'fast-loops'
const biggerThan2 = arrayFilter([1, 2, 3, 4], value => value > 2)
console.log(biggerThan2)
// => [3, 4]
arrayMap(arr, mapper)
Maps an array by running the mapper on each value.
Similar to Array.prototype.map
.
arr
(Array): The array that gets mappedmapper
(Function): The mapping method with the signature (value, index, length, arr) => newValue
import { arrayMap } from 'fast-loops'
const square = arrayMap([1, 2, 3, 4], value => value * value)
console.log(square)
// => [1, 4, 9, 16]
arrayReduce(arr, reducer, accumulator)
Reduces an array based on the accumulator.
Similar to Array.prototype.reduce
.
arr
(Array): The array that gets reducedreducer
(Function): The reducer method with the signature (accumulator, value, index, length, arr) => accumulator
accumulator
(any): The initial accumulator valueimport { arrayReduce } from 'fast-loops'
const sum = arrayReduce([1, 2, 3, 4], (out, value) => out + value, 0)
console.log(sum)
// => 10
objectEach(obj, iterator)
Iterates over each key in the object.
obj
(Object): The object to iterate overiterator
(Function): The iterator method with the signature (value, key, obj) => void
import { objectEach } from 'fast-loops'
objectEach({ 1: 10, 2: 20, 3: 30 }, console.log)
// 10, 1, { 1: 10, 2: 20, 3: 30 }
// 20, 2, { 1: 10, 2: 20, 3: 30 }
// 30, 3, { 1: 10, 2: 20, 3: 30 }
objectFilter(obj, filter)
Filters an object's keys according to the filter criteria.
obj
(Object): The object that gets filteredfilter
(Function): The filter method with the signature (value, key, obj) => boolean
import { objectFilter } from 'fast-loops'
const filter = (value, key) => value > 20 && parseInt(key) % 2 !== 0
const biggerThan20AndOddKey = objectFilter({ 1: 10, 2: 20, 3: 30, 4: 40 }, filter)
console.log(biggerThan20AndOddKey)
// => { 3: 30 }
objectFind(obj, query)
Tries to find a key-value pair that matches the query.
Returns the matching key or undefined
if none matches.
It's like Array.prototype.find
but for objects.
obj
(Object): The object that gets queriedquery
(Function): The query method with the signature (value, key, obj) => boolean
import { objectFind } from 'fast-loops'
const query = (value, key) => value > 20 && parseInt(key) % 2 === 0
const biggerThan20AndEvenKey = objectFind({ 1: 10, 2: 20, 3: 30, 4: 40 }, query)
console.log(biggerThan20AndEvenKey)
// => "4"
objectMap(obj, mapper)
Maps an object by running the mapper
on each value.
Similar to Object.keys(obj).map(mapper)
.
obj
(Object): The object that gets reducedmapper
(Function): The mapper method with the signature (value, key, obj) => newValue
import { objectMap } from 'fast-loops'
const mapped = objectMap({ 1: 10, 2: 20, 3: 30 }, (value, key) => value + parseInt(key))
console.log(mapped)
// => { 1: 11, 2: 22, 3: 33 }
objectReduce(obj, reducer, accumulator)
Reduces an object based on the accumulator.
obj
(Object): The object that gets reducedreducer
(Function): The reducer method with the signature (accumulator, value, key, obj) => accumulator
accumulator
(any): The initial accumulator valueimport { objectReduce } from 'fast-loops'
const sumOfValues = objectReduce({ 1: 10, 2: 20, 3: 30 }, (out, value) => out + value, 0)
console.log(sumOfValues)
// => 60
objectRenameKeys(obj, keys)
Renames object keys.
Uses objectReduce under the hood.
obj
(Object): The object that gets reducedkeys
(Object): The keys mapping an old key to a new keyimport { objectRenameKeys } from 'fast-loops'
const renamedObj = objectRenameKeys({ foo: 1, bar: 2 }, { foo: "baz" })
console.log(sumOfValues)
// => { baz: 1, bar: 2 }
objectMergeDeep(base, ...objs)
Recursively merges objects into a base object.
base
(Object): The base object which is changedobjs
(Array<Object>): A list of objects to be merged into the base objectimport { objectMergeDeep } from 'fast-loops'
const base = {
foo: 1,
bar: {
foo: 2
}
}
const mergedObj = objectMergeDeep(base, { baz: 3 }, { bar: { foo: 3 }})
console.log(mergedObj)
// => { foo: 1, bar: { foo: 3 }, baz: 3 }
While we support the module
key to support Tree Shaking, you might still want to import single methods without any overhead.
You can import every method using the full path to the method resource.
import objectReduce from 'fast-loops/lib/objectReduce'
fast-loops is licensed under the MIT License.
Documentation is licensed under Creative Common License.
Created with ♥ by @rofrischmann.
FAQs
Small, performant & immutable iteration utilities for Arrays and Objects
The npm package fast-loops receives a total of 742,832 weekly downloads. As such, fast-loops popularity was classified as popular.
We found that fast-loops demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.