Skip to content

docs: better naming and description for native-machine-id #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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,31 +1,32 @@
# @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();
console.log('SHA-256 Hashed Machine ID:', id);
const id = getMachineID({ raw: true });
console.log('Original Machine ID:', id);
console.log('SHA-256 Hashed Machine ID:', hashedId);
const rawId = getMachineID({ raw: true });
console.log('Original Machine ID:', rawId);
```

## Supported Platforms
Expand All @@ -36,21 +37,21 @@ console.log('Original Machine ID:', id);

## Comparison with `node-machine-id`

This module provides similar functionality to [node-machine-id](https://www.npmjs.com/package/node-machine-id), but **using native access to system APIs without the need for child processes**, making it much faster and reliable.
This module provides similar functionality to [node-machine-id](https://www.npmjs.com/package/node-machine-id) while **using native access to system APIs without the need for child processes**, making it much faster and reliable.

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",
"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.",
"name": "native-machine-id",
"version": "0.0.2",
"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 Expand Up @@ -58,5 +58,23 @@
"node-machine-id": "^1.1.12",
"typescript": "^5.0.4",
"ts-node": "^10.9.2"
}
},
"keywords": [
"machine id",
"node machine id",
"device identifier",
"node id",
"unique identifier",
"device id",
"machine identifier",
"unique id",
"platform id",
"electron id",
"node-machine-id",
"uuid",
"restrictions",
"native",
"telemetry",
"telemetry id"
]
}
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