An open API service indexing awesome lists of open source software.

https://github.com/roginvs/is-even

isEven function in native nodejs bindings using machine code
https://github.com/roginvs/is-even

Last synced: 9 months ago
JSON representation

isEven function in native nodejs bindings using machine code

Awesome Lists containing this project

README

          

# is-even

A deliberately inefficient utility to check if a number is even.

It literally contains [4 billion if statements](https://andreasjhkarlsson.github.io/jekyll/update/2023/12/27/4-billion-if-statements.html), one for every possible 32-bit unsigned integer.

## Features

- Works on macOS, Windows, and Linux
- Supports x64 and arm64
- Handles 32-bit unsigned numbers (0 to 0xFFFFFFFF). Passing anything else is undefined behavior
- Complexity: `O(n)`. Small numbers return fast, large ones can take 30–40s
- The 4 billion statements are generated on first import, not downloaded
- Generated code lives in `node_modules` (~45 GB on amd64)
- Uses Node.js native bindings to execute assembly
- No external dependencies

## Installation

```sh
npm install https://github.com/roginvs/is-even
```

## Usage

```js
const isEven = require("is-even");

console.log(isEven(4)); // true
console.log(isEven(7)); // false
```

## TODO

- Add support for risc-v
- Add `bun` support

## License

MIT