Skip to content

Commit f2a3c1b

Browse files
committed
Performance boost --all-native
1 parent 2f116a8 commit f2a3c1b

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

cppsrc/main.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ int nodeID(std::string macID)
5353

5454
// ////////////////////////////////////////////////////////////////////////////////////////
5555

56+
uint64_t getCurrentTime()
57+
{
58+
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
59+
}
60+
61+
// ////////////////////////////////////////////////////////////////////////////////////////
62+
5663
class Snowflake : public Napi::ObjectWrap<Snowflake>
5764
{
5865
public:
@@ -62,6 +69,7 @@ class Snowflake : public Napi::ObjectWrap<Snowflake>
6269
private:
6370
static Napi::FunctionReference constructor;
6471
uint64_t _lastTimestamp;
72+
uint64_t _CUSTOM_EPOCH;
6573
int _sequence;
6674
std::string _macID;
6775
int _nodeID;
@@ -90,10 +98,13 @@ Napi::Object Snowflake::Init(Napi::Env env, Napi::Object exports)
9098
Snowflake::Snowflake(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Snowflake>(info)
9199
{
92100
Napi::String value = info[0].As<Napi::String>();
101+
Napi::Number EPOCH = info[1].As<Napi::Number>();
102+
93103
this->_macID = value.Utf8Value();
94104
this->_nodeID = nodeID(this->_macID);
95105
this->_lastTimestamp = 0;
96106
this->_sequence = 0;
107+
this->_CUSTOM_EPOCH = EPOCH.Int64Value();
97108
}
98109

99110
Napi::FunctionReference Snowflake::constructor;
@@ -112,15 +123,16 @@ Napi::Value Snowflake::getUniqueIDBigInt(const Napi::CallbackInfo &info)
112123
{
113124
Napi::Env env = info.Env();
114125

115-
Napi::Number first = info[0].As<Napi::Number>();
116-
117-
uint64_t currentTimestamp = first.Int64Value();
126+
uint64_t currentTimestamp = getCurrentTime();
118127

119128
if (currentTimestamp == this->_lastTimestamp)
120129
{
121130
this->_sequence = (this->_sequence + 1) & maxSequence;
122131
if (this->_sequence == 0)
123-
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
132+
{
133+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
134+
currentTimestamp++;
135+
}
124136
}
125137
else
126138
{
@@ -143,9 +155,7 @@ Napi::Value Snowflake::getUniqueID(const Napi::CallbackInfo &info)
143155
{
144156
Napi::Env env = info.Env();
145157

146-
Napi::Number first = info[0].As<Napi::Number>();
147-
148-
uint64_t currentTimestamp = first.Int64Value();
158+
uint64_t currentTimestamp = getCurrentTime() - this->_CUSTOM_EPOCH;
149159

150160
if (currentTimestamp == this->_lastTimestamp)
151161
{

lib/generateUniqueID.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/generateUniqueID.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodejs-snowflake",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "Generate time sortable 64 bits unique ids for distributed systems (inspired from twitter snowflake)",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/generateUniqueID.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ export class UniqueID {
4141
this._MACID = macIDString;
4242
this._FORMATTEDMACID = macID;
4343
if (!this._MACID) throw new Error('No MAC ADDRESS found to initialise');
44-
this._snowflake = new Snowflake(this._MACID);
44+
this._snowflake = new Snowflake(this._MACID, this._CUSTOM_EPOCH);
4545
this._nextID = config.returnNumber
46-
? (timestamp: number) => this._snowflake.getUniqueIDBigInt(timestamp)
47-
: (timestamp: string) => this._snowflake.getUniqueID(timestamp)
46+
? () => this._snowflake.getUniqueIDBigInt()
47+
: () => this._snowflake.getUniqueID()
4848
}
4949

5050
/**
5151
* Generates a unique time sortable 64 bit number using native code
5252
* @returns {string | bigint} the unique id
5353
*/
5454
getUniqueID(): string | bigint {
55-
return this._nextID(Date.now() - this._CUSTOM_EPOCH);
55+
return this._nextID();
5656
}
5757

5858
/**

0 commit comments

Comments
 (0)