Skip to content

Commit

Permalink
chore: improve delete and evict methods (#30)
Browse files Browse the repository at this point in the history
* chore: improve `delete` and `evict`

* ci: add Code QL

* chore: improve `setTail`

* chore: lint

* chore: avoid `undefined` key

* docs: update docs

* chore: improve benchmarks

* ci: add benchmark workflow

* chore: ensure benchmark failure

* docs: improve benchmark

* chore: update dependencies
  • Loading branch information
wellwelwel authored Aug 27, 2024
1 parent 3bd7033 commit d716377
Show file tree
Hide file tree
Showing 14 changed files with 612 additions and 204 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/ci_benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: '🎖️ CI — Benchmark'

on:
pull_request:
workflow_dispatch:

jobs:
benchmark:
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
fail-fast: false
name: Compare
steps:
- name: ➕ Actions - Checkout
uses: actions/checkout@v4

- name: ➕ Actions - Setup Node.js
uses: actions/setup-node@v4
with:
check-latest: true
node-version: lts/*

- name: ➕ Cache dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: npm-linux-${{ hashFiles('package-lock.json') }}
restore-keys: npm-linux-

- name: ➕ Cache dependencies (Benchmark)
uses: actions/cache@v4
with:
path: ~/.npm
key: npm-benchmark-${{ hashFiles('benchmark/package-lock.json') }}
restore-keys: npm-benchmark-

- name: 📦 Installing Dependencies
run: npm ci

- name: 🚀 Building Poku
run: npm run build

- name: 🎖️ Rock it (ESM)
run: npm run benchmark:esm

- name: 🎖️ Rock it (CJS)
run: npm run benchmark:cjs
80 changes: 80 additions & 0 deletions .github/workflows/ci_codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: '🔎 CI — CodeQL'

on:
push:
branches:
- 'main'
pull_request:
workflow_dispatch:

jobs:
analyze:
name: Analyze
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners
# Consider using larger runners for possible analysis time improvements.
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: ['javascript-typescript']
# CodeQL supports [ 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' ]
# Use only 'java-kotlin' to analyze code written in Java, Kotlin or both
# Use only 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality
config-file: ./.github/codeql/codeql-config.yml

# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: '/language:${{matrix.language}}'
55 changes: 15 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![GitHub Workflow Status (Bun)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_bun.yml?event=push&label=&branch=main&logo=bun&logoColor=ffffff&color=f368e0)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_bun.yml?query=branch%3Amain)
[![GitHub Workflow Status (Deno)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_deno.yml?event=push&label=&branch=main&logo=deno&logoColor=ffffff&color=079992)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_deno.yml?query=branch%3Amain)

🔥 An extremely fast and efficient <strong><a href="https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29">LRU</a> Cache</strong> for <strong>JavaScript</strong> (<strong>Browser</strong> compatible) — **6.4KB**.
🔥 An extremely fast and efficient <strong><a href="https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29">LRU</a> Cache</strong> for <strong>JavaScript</strong> (<strong>Browser</strong> compatible) — **6.7KB**.

</div>

Expand Down Expand Up @@ -87,6 +87,8 @@ Adds a key-value pair to the cache. Updates the value if the key already exists
LRU.set('key', 'value');
```

> `undefined` keys will simply be ignored.
### Get a cache

Retrieves the value for a given key and moves the key to the most recent position.
Expand Down Expand Up @@ -207,56 +209,29 @@ LRU.forEach((key, value) => {

The benchmark is performed by comparing `1,000,000` runs through a maximum cache limit of `100,000`, getting `333,333` caches and delenting `200,000` keys 10 consecutive times, clearing the cache every run.

> You can see how the tests are run and compared in the [benchmark](https://github.com/wellwelwel/lru.min/tree/main/benchmark) directory.
>
> - [**lru-cache**](https://github.com/isaacs/node-lru-cache) `v11.0.0`
> - [**quick-lru**](https://github.com/sindresorhus/quick-lru) `v7.0.0`
#### Node.js

```sh
# ES Modules
lru.min: 247.45ms
lru-cache: 255.93ms
quick-lru: 312.90ms
```

```sh
# CommonJS
lru.min: 236.35ms
lru-cache: 258.74ms
quick-lru: not compatible
```

#### Bun

```sh
# ES Modules
lru.min: 298.42ms
quick-lru: 315.37ms
lru-cache: 359.23ms
```

```sh
# CommonJS
lru.min: 363.79ms
lru-cache: 371.39ms
quick-lru: not compatible
# Time:
lru.min: 240.45ms
lru-cache: 258.32ms
quick-lru: 279.89ms

# CPU:
lru.min: 275558.30µs
lru-cache: 306858.30µs
quick-lru: 401318.80µs
```

#### Deno

```sh
# ES Modules
lru.min: 222.60ms
lru-cache: 227.80ms
quick-lru: 253.00ms
```
- See detailed results and how the tests are run and compared in the [benchmark](https://github.com/wellwelwel/lru.min/tree/main/benchmark) directory.

---

## Security Policy

[![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/wellwelwel/lru.min/ci_codeql.yml?event=push&label=&branch=main&logo=github&logoColor=white&color=f368e0)](https://github.com/wellwelwel/lru.min/actions/workflows/ci_codeql.yml?query=branch%3Amain)

Please check the [**SECURITY.md**](https://github.com/wellwelwel/lru.min/blob/main/SECURITY.md).

---
Expand Down
92 changes: 92 additions & 0 deletions benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Benchmark

The benchmark is performed by comparing `1,000,000` runs through a maximum cache limit of `100,000`, getting `333,333` caches and delenting `200,000` keys 10 consecutive times, clearing the cache every run.

> - [**lru-cache**](https://github.com/isaacs/node-lru-cache) `v11.0.0`
> - [**quick-lru**](https://github.com/sindresorhus/quick-lru) `v7.0.0`
#### Node.js

- ES Modules

```sh
# Time:
lru.min: 240.45ms
lru-cache: 258.32ms
quick-lru: 279.89ms

# CPU:
lru.min: 275558.30µs
lru-cache: 306858.30µs
quick-lru: 401318.80µs
```

- CommonJS

```sh
# Time:
lru.min: 242.86ms
lru-cache: 264.30ms
quick-lru: not compatible

# CPU:
lru.min: 280118.00µs
lru-cache: 310327.20µs
quick-lru: not compatible
```

#### Bun

- ES Modules

```sh
# Time:
lru.min: 298.42ms
quick-lru: 315.37ms
lru-cache: 359.23ms

# CPU:
lru.min: 401869.50µs
quick-lru: 478517.40µs
lru-cache: 510357.60µs
```

- CommonJS

```sh
# Time:
lru.min: 324.41ms
lru-cache: 370.22ms
quick-lru: not compatible

# CPU:
lru.min: 396790.50µs
lru-cache: 488574.50µs
quick-lru: not compatible
```

#### Deno

- ES Modules

```sh
# Time:
lru.min: 222.60ms
lru-cache: 227.80ms
quick-lru: 253.00ms
```

> **Deno** benchmarks were carried out without an isolated process.
---

## Running

To run the benchmark tests, follow these steps in the `./lru.min` directory:

```sh
npm ci
npm run build
npm run benchmark:esm
npm run benchmark:cjs
```
Loading

0 comments on commit d716377

Please sign in to comment.