Skip to content

Commit 0a00011

Browse files
authored
Optimize {__proto__:null} object literal creation (#1118)
Speeds up the common pattern of creating prototype-less objects by 40%: TEST N TIME (ns) REF (ns) SCORE (%) object_null 20000 124.50 TEST N TIME (ns) REF (ns) SCORE (%) object_null 50000 74.30 It's so much faster the benchmarker decides to run it 50,000 times instead of merely 20,000 times. Win.
1 parent 36f969d commit 0a00011

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

quickjs.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16430,6 +16430,7 @@ typedef enum {
1643016430
OP_SPECIAL_OBJECT_HOME_OBJECT,
1643116431
OP_SPECIAL_OBJECT_VAR_OBJECT,
1643216432
OP_SPECIAL_OBJECT_IMPORT_META,
16433+
OP_SPECIAL_OBJECT_NULL_PROTO,
1643316434
} OPSpecialObjectEnum;
1643416435

1643516436
#define FUNC_RET_AWAIT 0
@@ -16729,6 +16730,11 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1672916730
if (unlikely(JS_IsException(sp[-1])))
1673016731
goto exception;
1673116732
break;
16733+
case OP_SPECIAL_OBJECT_NULL_PROTO:
16734+
*sp++ = JS_NewObjectProtoClass(ctx, JS_NULL, JS_CLASS_OBJECT);
16735+
if (unlikely(JS_IsException(sp[-1])))
16736+
goto exception;
16737+
break;
1673216738
default:
1673316739
abort();
1673416740
}
@@ -33471,6 +33477,18 @@ static __exception int resolve_labels(JSContext *ctx, JSFunctionDef *s)
3347133477
}
3347233478
goto no_change;
3347333479

33480+
case OP_object:
33481+
if (code_match(&cc, pos_next, OP_null, OP_set_proto, -1)) {
33482+
if (cc.line_num >= 0) line_num = cc.line_num;
33483+
if (cc.col_num >= 0) col_num = cc.col_num;
33484+
add_pc2line_info(s, bc_out.size, line_num, col_num);
33485+
dbuf_putc(&bc_out, OP_special_object);
33486+
dbuf_putc(&bc_out, OP_SPECIAL_OBJECT_NULL_PROTO);
33487+
pos_next = cc.pos;
33488+
break;
33489+
}
33490+
goto no_change;
33491+
3347433492
default:
3347533493
no_change:
3347633494
add_pc2line_info(s, bc_out.size, line_num, col_num);

tests/microbench.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,15 @@ function math_min(n)
710710
return n * 1000;
711711
}
712712

713+
function object_null(n)
714+
{
715+
var j;
716+
for(j = 0; j < n; j++) {
717+
global_res = {__proto__: null};
718+
}
719+
return n;
720+
}
721+
713722
function regexp_ascii(n)
714723
{
715724
var i, j, r, s;
@@ -1098,6 +1107,7 @@ function main(argc, argv, g)
10981107
array_for_in,
10991108
array_for_of,
11001109
math_min,
1110+
object_null,
11011111
regexp_ascii,
11021112
regexp_utf16,
11031113
string_build1,

0 commit comments

Comments
 (0)