Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 93 additions & 63 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
# @mongodb-js/machine-id
# native-machine-id

> Native implementation for retrieving unique machine ID without admin privileges or child processes for desktop platforms. Faster and more reliable alternative to node-machine-id.
> Native retrieval of a unique desktop machine ID without admin privileges or child processes. Faster and more reliable alternative to node-machine-id.

## Installation

```
npm install @mongodb-js/machine-id
npm install native-machine-id
```

Or use it directly in the CLI

```
npx @mongodb-js/machine-id
npx native-machine-id
npx native-machine-id --raw
```

## Usage

### As a module

```javascript
import { getMachineID } from '@mongodb-js/machine-id';
import { getMachineID } from 'native-machine-id';

// Get the machine ID
const hashedId = getMachineID();
Expand All @@ -40,17 +41,17 @@ This module provides similar functionality to [node-machine-id](https://www.npmj

Here's a table of performance comparisons between the two libraries, based on the average runtime from 1000 iterations of the `getMachineId` and `machineIdSync` functions, from `scripts/benchmark.ts`:

| Test | node-machine-id | @mongodb-js/machine-id | Improvement |
| ----------- | --------------- | ---------------------- | ----------- |
| Test | node-machine-id | native-machine-id | Improvement |
| ----------- | --------------- | ----------------- | ----------- |
| **Mac** |
| Raw | 10.71ms | 0.0072ms | 1494x |
| Hashed | 12.42ms | 0.0176ms | 707x |
| Raw | 10.71ms | 0.0072ms | 1494x |
| Hashed | 12.42ms | 0.0176ms | 707x |
| **Linux** |
| Raw | 3.26ms | 0.0059ms | 557x |
| Hashed | 3.25ms | 0.0088ms | 368x |
| Raw | 3.26ms | 0.0059ms | 557x |
| Hashed | 3.25ms | 0.0088ms | 368x |
| **Windows** |
| Raw | 45.36ms\* | 0.0122ms | 3704x |
| Hashed | 28.66ms\* | 0.0272ms | 1053x |
| Raw | 45.36ms\* | 0.0122ms | 3704x |
| Hashed | 28.66ms\* | 0.0272ms | 1053x |

\* - Windows tests may be inaccurate due to potential caching.

Expand All @@ -60,7 +61,7 @@ If you were previously using `node-machine-id`, you can use the following mappin

```ts
import { createHash } from 'crypto';
import { getMachineId } from '@mongodb-js/machine-id';
import { getMachineId } from 'native-machine-id';

function machineIdSync(original: boolean): string | undefined {
const rawMachineId = getMachineId({ raw: true }).toLowerCase();
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mongodb-js/machine-id",
"name": "native-machine-id",
"version": "1.1.0",
"description": "Native implementation for retrieving unique machine ID without admin privileges or child processes for desktop platforms. Faster and more reliable alternative to node-machine-id.",
"description": "Native retrieval of a unique desktop machine ID without admin privileges or child processes. Faster and more reliable alternative to node-machine-id.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Performance comparison script for machine-id vs node-machine-id
*
* This script measures and compares the performance of @mongodb-js/machine-id
* This script measures and compares the performance of native-machine-id
* against the node-machine-id package.
*/

Expand Down Expand Up @@ -58,21 +58,21 @@ function runBenchmark() {
const otherTimeRaw = Number(endOtherRaw - startOtherRaw) / 1_000_000; // ms

console.log(
`@mongodb-js/machine-id: ${formatTime(ourTimeRaw)} total, ${formatTime(ourTimeRaw / ITERATIONS)} per call`,
`native-machine-id: ${formatTime(ourTimeRaw)} total, ${formatTime(ourTimeRaw / ITERATIONS)} per call`,
);
console.log(
`node-machine-id: ${formatTime(otherTimeRaw)} total, ${formatTime(otherTimeRaw / ITERATIONS)} per call`,
);
console.log(
`Comparison: @mongodb-js/machine-id is ${formatComparison(ourTimeRaw, otherTimeRaw)}`,
`Comparison: native-machine-id is ${formatComparison(ourTimeRaw, otherTimeRaw)}`,
);

console.log('----------------------------------------');

// Test hashed mode
console.log('Hashed:');

// @mongodb-js/machine-id
// native-machine-id
const startOursHashed = process.hrtime.bigint();
for (let i = 0; i < ITERATIONS; i++) {
getMachineId();
Expand All @@ -89,13 +89,13 @@ function runBenchmark() {
const otherTimeHashed = Number(endOtherHashed - startOtherHashed) / 1_000_000; // ms

console.log(
`@mongodb-js/machine-id: ${formatTime(ourTimeHashed)} total, ${formatTime(ourTimeHashed / ITERATIONS)} per call`,
`native-machine-id: ${formatTime(ourTimeHashed)} total, ${formatTime(ourTimeHashed / ITERATIONS)} per call`,
);
console.log(
`node-machine-id: ${formatTime(otherTimeHashed)} total, ${formatTime(otherTimeHashed / ITERATIONS)} per call`,
);
console.log(
`Comparison: @mongodb-js/machine-id is ${formatComparison(ourTimeHashed, otherTimeHashed)}`,
`Comparison: native-machine-id is ${formatComparison(ourTimeHashed, otherTimeHashed)}`,
);
}

Expand Down