1

This is a JavaScript source code.

const obj = {
    name: 'hello',
    age: 12
};

console.log(obj);

And this is a Google V8 byte code generated using node js option --print-bytecode.

[generated bytecode for function: ]
Parameter count 6
Frame size 12
    0 E> 17543D4A @    0 : a0                StackCheck 
   12 S> 17543D4B @    1 : 79 00 00 29 fa    CreateObjectLiteral [0], [0], #41, r1
         17543D50 @    6 : 27 fa fb          Mov r1, r0
   54 S> 17543D53 @    9 : 13 01 01          LdaGlobal [1], [1]
         17543D56 @   12 : 26 f9             Star r2
   62 E> 17543D58 @   14 : 28 f9 02 03       LdaNamedProperty r2, [2], [3]
         17543D5C @   18 : 26 fa             Star r1
   62 E> 17543D5E @   20 : 57 fa f9 fb 05    CallProperty1 r1, r2, r0, [5]
         17543D63 @   25 : 0d                LdaUndefined 
   70 S> 17543D64 @   26 : a4                Return 
Constant pool (size = 3)
17543CFD: [FixedArray] in OldSpace
 - map: 0x3a0841e5 <Map>
 - length: 3
           0: 0x17543cdd <BoilerplateDescription[4]>
           1: 0x3a6927a1 <String[7]: console>
           2: 0x3a685b69 <String[3]: log>
Handler Table (size = 0)

Any help would be thankful.

ZhefengJin
  • 930
  • 8
  • 17

1 Answers1

1

As the name implies, CreateObjectLiteral creates an object from a literal, in this case {name: 'hello', ...}. To make this more efficient, V8 uses what it calls "boilerplate" objects, which are partially populated "blueprints" that can quickly be copied to create a new instance; after that any dynamic properties (i.e., that aren't compile-time constants) are filled in. You don't have any of those in this example, so you don't see that. Observe the difference to the following, slightly modified example:

function foo(n, a) {
    const obj = {name: n, age: a};
    console.log(obj);
}

foo("hello", 12);
jmrk
  • 34,271
  • 7
  • 59
  • 74
  • Thank you very much for your answer. In "CreateObjectLiteral [0], [0], #41, r1" what are [0], [0], #41, r1? – ZhefengJin Apr 14 '20 at 18:31
  • The bytecode's operands. See `BytecodeGraphBuilder::VisitCreateObjectLiteral`. [0] is the constant pool entry for the boilerplate, [0] is the slot in the type feedback vector, #41 is a bit field with flags, and r1 is the target register. – jmrk Apr 15 '20 at 11:31