Sign In

offload-fn

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

offload-fn

Run a function in a Worker thread and get back a promise

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

offload-fn

Run a function in a Worker thread and get back a promise

Install

npm install offload-fn

Usage

import offloadFunction from 'offload-fn';

const result = await offloadFunction((a, b) => a + b, 2, 3);
//=> 5

// CPU-intensive work
const sum = await offloadFunction(n => {
	let total = 0;
	for (let i = 0; i < n; i++) {
		total += i;
	}
	return total;
}, 1_000_000);

API

offloadFunction(function_, ...arguments_)

Returns a Promise that resolves with the function's return value.

Serializes the function to a string and runs it inside a Worker thread. Arguments and return values must be serializable via the structured clone algorithm.

function_

Type: Function

The function to execute in a Worker thread.

arguments_

Type: unknown[]

Arguments to pass to the function.

Limitations

  • The function cannot reference closure variables — it is serialized to a string.
  • Arguments must be serializable (structured clone).
  • The return value must be serializable (structured clone).
  • perf-fn - Measure function execution time

License

MIT

Keywords

worker

FAQs

Package last updated on 16 Feb 2026

Did you know?

Socket

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.

Install

Related posts