npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fast-keys

v0.2.0

Published

[![Build Status](https://travis-ci.org/b-gran/fast-keys.svg?branch=master)](https://travis-ci.org/b-gran/fast-keys) [![npm version](https://badge.fury.io/js/fast-keys.svg)](https://badge.fury.io/js/fast-keys) [![GitHub stars](https://img.shields.io/github

Downloads

5

Readme

🔑 fast-keys

Build Status npm version GitHub stars

fast-keys is a highly performant alternative to Object.keys. Its API has many of the same methods as Array.prototype, plus a few additional helpers.

  • ⏩ 🏎 Really, really fast
  • 🧠 Less heap allocation and garbage collection than Object.keys
  • 🛠️ Extra utility methods

Usage

import K from 'fast-keys'

const object = { foo: 1, bar: 1, baz: 1 }
console.log(K(object).length) // 3
console.log(K(object).map(string => string + '2')) // [ 'foo2', 'bar2', 'baz2' ]
console.log(K(object).toSet()) // Set { 'foo', 'bar', 'baz' }

fast-keys has many of the Array.prototype methods, and some extra helpers too.

Why should I use this?

Object.keys has a significant performance cost. It's extremely common to use Object.keys to determine the number of keys in an object:

const object = { foo: 1, bar: 1, baz: 1 }
const keyLength = Object.keys(object).length

In order to determine the key length, the runtime will allocate an array containing all of the object's keys and then return the length of the array. For applications where performance is important, the cost of these quickly array allocations adds up. Not only does is the array allocation itself expensive, additional garbage collection is needed to clean up the arrays afterward.

Of course, it's possible to do explicit object key iteration in performance-critical applications, but by doing so we aren't able to use many of the useful functional tools available in the Javascript ecosystem.

This library provides the best of both worlds: the familiar functional-style Object.keys and Array.prototype interfaces without the performance costs of unnecessary Array allocations.

How fast is it?

It's about 8-10x faster than using Object.keys, depending on the properties of the objects and the Array.prototype methods. It also uses much less memory and requires less garbage collection, so the performance might be even better depending on your application.

Benchmarks

https://jsperf.com/fast-keys

Performance

API Reference

function K()

The default export of the module.

Accepts any non-nil value and returns a IFastKeys instance.

function K <ObjectType> (object: ObjectType): IFastKeys<ObjectType>

IFastKeys

The interface that exposes the Array.prototype methods.

interface IFastKeys <ObjectType> {
    length: number;

    some(iteratee: Predicate<ObjectType>): boolean
    map<A>(iteratee: (key: keyof ObjectType) => A): Array<A>
    toSet(): Set<keyof ObjectType>
    every(iteratee: Predicate<ObjectType>): boolean
    filter(iteratee: Predicate<ObjectType>): Array
    forEach(iteratee: (key: keyof ObjectType) => void): void
    find(predicate: Predicate<ObjectType>): keyof ObjectType | undefined
}

type Predicate <ObjectType> = (key: keyof ObjectType) => boolean

length

some()

map()

toSet()

every()

filter()

forEach()

find()

IFastKeys predicates are only passed the key and are not passed the index or the entire array of keys.

All functions only operate on keys that are own, enumerable, and string. This is the same behavior as Object.keys. Non-enumerable, inherited, and Symbol keys will be skipped.

IFastKeys instances don't cache or pre-compute the results of any calls, so you can use the same IFastKeys instance even if the underlying object changes. For example:

import K from 'fast-keys'

const object = { foo:1 }
const fkObject = K(object)

console.log(fkObject.length) // 1

object.bar = 1

console.log(fkObject.length) // 2

IFastKeys.length

MDN The number of own (i.e. non-inherited) enumerable string keys of the object.

IFastKeys.some

MDN Returns true if at least one key satisfies the predicate. Short-circuits and returns immediately if a key satisfies the predicate.

function some (iteratee: (key: string) => boolean): boolean

IFastKeys.map

MDN Returns a new Array containing the results of applying the iteratee to each key in the object.

function map <A> (iteratee: (key: string) => A): Array<A>

IFastKeys.toSet

Returns a Set containing the object's keys. Equivalent to new Set(Object.keys(object)).

function toSet (): Set<string>

IFastKeys.every

MDN Returns true if every key satisfies the predicate. Short-circuits and returns immediately if a key fails.

function every (iteratee: (key: string) => boolean): boolean

IFastKeys.filter

MDN Returns a new Array containing only the keys that satisfy the predicate.

function filter (iteratee: (key: string) => boolean): Array<string>

IFastKeys.forEach

MDN Execute some function for each key in the object. An opportunity to do side effects for each key.

function forEach (iteratee: (key: string) => void): void

IFastKeys.find

MDN Returns the first key that satisfies the predicate, or returns undefined. Short-circuits and returns immediately if a matching key is found.

function find (iteratee: (key: string) => boolean): string | undefined