Skip to content

Commit 9a421b3

Browse files
author
Fabrice Bellard
committed
optimized Array.prototype.push
1 parent 3e5f2bb commit 9a421b3

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

quickjs.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41533,6 +41533,31 @@ static JSValue js_array_push(JSContext *ctx, JSValueConst this_val,
4153341533
int i;
4153441534
int64_t len, from, newLen;
4153541535

41536+
if (likely(JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT && !unshift)) {
41537+
JSObject *p = JS_VALUE_GET_OBJ(this_val);
41538+
if (likely(p->class_id == JS_CLASS_ARRAY && p->fast_array &&
41539+
p->extensible &&
41540+
p->shape->proto == JS_VALUE_GET_OBJ(ctx->class_proto[JS_CLASS_ARRAY]) &&
41541+
ctx->std_array_prototype &&
41542+
JS_VALUE_GET_TAG(p->prop[0].u.value) == JS_TAG_INT &&
41543+
JS_VALUE_GET_INT(p->prop[0].u.value) == p->u.array.count &&
41544+
(get_shape_prop(p->shape)->flags & JS_PROP_WRITABLE) != 0)) {
41545+
/* fast case */
41546+
uint32_t new_len;
41547+
new_len = p->u.array.count + argc;
41548+
if (likely(new_len <= INT32_MAX)) {
41549+
if (unlikely(new_len > p->u.array.u1.size)) {
41550+
if (expand_fast_array(ctx, p, new_len))
41551+
return JS_EXCEPTION;
41552+
}
41553+
for(i = 0; i < argc; i++)
41554+
p->u.array.u.values[p->u.array.count + i] = JS_DupValue(ctx, argv[i]);
41555+
p->prop[0].u.value = JS_NewInt32(ctx, new_len);
41556+
p->u.array.count = new_len;
41557+
return JS_NewInt32(ctx, new_len);
41558+
}
41559+
}
41560+
}
4153641561
obj = JS_ToObject(ctx, this_val);
4153741562
if (js_get_length64(ctx, &len, obj))
4153841563
goto exception;

0 commit comments

Comments
 (0)