Skip to content

Commit 547cab9

Browse files
ckerrruyadorno
authored andcommitted
src: use NewFromUtf8Literal in NODE_DEFINE_CONSTANT
Small efficiency improvement over NewFromUtf8(): the literal's length is known at compile time, so V8 doesn't have to call strlen() or ToLocalChecked(). PR-URL: #55581 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Richard Lau <rlau@redhat.com>
1 parent bf68733 commit 547cab9

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

src/node.h

+32-38
Original file line numberDiff line numberDiff line change
@@ -1023,44 +1023,38 @@ NODE_DEPRECATED("Use v8::Date::ValueOf() directly",
10231023
})
10241024
#define NODE_V8_UNIXTIME node::NODE_V8_UNIXTIME
10251025

1026-
#define NODE_DEFINE_CONSTANT(target, constant) \
1027-
do { \
1028-
v8::Isolate* isolate = target->GetIsolate(); \
1029-
v8::Local<v8::Context> context = isolate->GetCurrentContext(); \
1030-
v8::Local<v8::String> constant_name = \
1031-
v8::String::NewFromUtf8(isolate, #constant, \
1032-
v8::NewStringType::kInternalized).ToLocalChecked(); \
1033-
v8::Local<v8::Number> constant_value = \
1034-
v8::Number::New(isolate, static_cast<double>(constant)); \
1035-
v8::PropertyAttribute constant_attributes = \
1036-
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); \
1037-
(target)->DefineOwnProperty(context, \
1038-
constant_name, \
1039-
constant_value, \
1040-
constant_attributes).Check(); \
1041-
} \
1042-
while (0)
1043-
1044-
#define NODE_DEFINE_HIDDEN_CONSTANT(target, constant) \
1045-
do { \
1046-
v8::Isolate* isolate = target->GetIsolate(); \
1047-
v8::Local<v8::Context> context = isolate->GetCurrentContext(); \
1048-
v8::Local<v8::String> constant_name = \
1049-
v8::String::NewFromUtf8(isolate, #constant, \
1050-
v8::NewStringType::kInternalized) \
1051-
.ToLocalChecked(); \
1052-
v8::Local<v8::Number> constant_value = \
1053-
v8::Number::New(isolate, static_cast<double>(constant)); \
1054-
v8::PropertyAttribute constant_attributes = \
1055-
static_cast<v8::PropertyAttribute>(v8::ReadOnly | \
1056-
v8::DontDelete | \
1057-
v8::DontEnum); \
1058-
(target)->DefineOwnProperty(context, \
1059-
constant_name, \
1060-
constant_value, \
1061-
constant_attributes).Check(); \
1062-
} \
1063-
while (0)
1026+
#define NODE_DEFINE_CONSTANT(target, constant) \
1027+
do { \
1028+
v8::Isolate* isolate = target->GetIsolate(); \
1029+
v8::Local<v8::Context> context = isolate->GetCurrentContext(); \
1030+
v8::Local<v8::String> constant_name = v8::String::NewFromUtf8Literal( \
1031+
isolate, #constant, v8::NewStringType::kInternalized); \
1032+
v8::Local<v8::Number> constant_value = \
1033+
v8::Number::New(isolate, static_cast<double>(constant)); \
1034+
v8::PropertyAttribute constant_attributes = \
1035+
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); \
1036+
(target) \
1037+
->DefineOwnProperty( \
1038+
context, constant_name, constant_value, constant_attributes) \
1039+
.Check(); \
1040+
} while (0)
1041+
1042+
#define NODE_DEFINE_HIDDEN_CONSTANT(target, constant) \
1043+
do { \
1044+
v8::Isolate* isolate = target->GetIsolate(); \
1045+
v8::Local<v8::Context> context = isolate->GetCurrentContext(); \
1046+
v8::Local<v8::String> constant_name = v8::String::NewFromUtf8Literal( \
1047+
isolate, #constant, v8::NewStringType::kInternalized); \
1048+
v8::Local<v8::Number> constant_value = \
1049+
v8::Number::New(isolate, static_cast<double>(constant)); \
1050+
v8::PropertyAttribute constant_attributes = \
1051+
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete | \
1052+
v8::DontEnum); \
1053+
(target) \
1054+
->DefineOwnProperty( \
1055+
context, constant_name, constant_value, constant_attributes) \
1056+
.Check(); \
1057+
} while (0)
10641058

10651059
// Used to be a macro, hence the uppercase name.
10661060
inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,

0 commit comments

Comments
 (0)