# fast-write-atomic

[![CI](https://github.com/mcollina/fast-write-atomic/actions/workflows/ci.yml/badge.svg)](https://github.com/mcollina/fast-write-atomic/actions/workflows/ci.yml)
[![npm version](https://img.shields.io/npm/v/fast-write-atomic.svg)](https://www.npmjs.com/package/fast-write-atomic)

Fast way to write a file atomically, for Node.js.

## Requirements

- Node.js 20+

## Install

```bash
npm i fast-write-atomic
```

## Example

```js
const writeFile = require('fast-write-atomic')

const data = Buffer.from('hello world')

writeFile('./hello', data, function (err) {
  if (err) {
    console.log(err)
    return
  }

  console.log('file written')
})
```

## Promise API

`writeFile.promise(path, content)` returns a Promise and keeps the same atomicity behavior.

```js
const writeFile = require('fast-write-atomic')

await writeFile.promise('./hello', Buffer.from('hello world'))
```

## Development

```bash
npm install
npm test
npm run cov
node bench.js
```

## Benchmarks

Those benchmarks write a 1 MB file a thousand times:

```text
benchWriteFileAtomic*1000: 9830.501ms
benchFastWriteAtomic*1000: 8848.916ms
benchWriteFileAtomic*1000: 9944.722ms
benchFastWriteAtomic*1000: 8997.108ms
```

## License

MIT
