1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16declare function print(arg:any) : string; 17declare interface ArkTools { 18 timeInUs(arg:any):number 19} 20 21function testDefineProperty() { 22 let start = ArkTools.timeInUs(); 23 for (let i = 0; i < 100_000; i++) { 24 const o = {}; 25 Object.defineProperty(o, "a", { 26 value: 1, 27 enumerable: false, 28 configurable: false, 29 writable: false, 30 value: "static", 31 }); 32 Object.defineProperty(o, "b", { 33 value: 2, 34 writable: true, 35 enumerable: true, 36 configurable: true, 37 }); 38 Object.defineProperty(o, "c", { 39 value: 3, 40 writable: false, 41 }); 42 } 43 let end = ArkTools.timeInUs(); 44 let time = (end - start) / 1000 45 print("Object DefineProperty:\t"+String(time)+"\tms"); 46} 47 48function testHasOwnProperty() { 49 const example = {}; 50 example.prop = "exists"; 51 let start = ArkTools.timeInUs(); 52 for (let i = 0; i < 1_000_000; i++) { 53 example.hasOwnProperty("prop"); 54 } 55 let end = ArkTools.timeInUs(); 56 let time = (end - start) / 1000 57 print("Object HasOwnProperty:\t"+String(time)+"\tms"); 58} 59 60function testAssign() { 61 let o1 = { a: 1 }; 62 let o2 = { b: 2 }; 63 let o3 = { c: 3 }; 64 let res; 65 let start = ArkTools.timeInUs(); 66 for (let i = 0; i < 1_000_000; i++) { 67 res = Object.assign(o1, o2, o3); 68 } 69 let end = ArkTools.timeInUs(); 70 let time = (end - start) / 1000 71 print(res); 72 print("Object Assign:\t"+String(time)+"\tms"); 73} 74 75function testCreate() { 76 const person = { 77 isHuman: false, 78 printIntroduction: function () { 79 }, 80 }; 81 let res; 82 let start = ArkTools.timeInUs(); 83 for (let i = 0; i < 1_000_000; i++) { 84 res = Object.create(person); 85 } 86 let end = ArkTools.timeInUs(); 87 let time = (end - start) / 1000 88 print(res); 89 print("Object Create:\t"+String(time)+"\tms"); 90} 91 92class Dog { 93 constructor(name, breed, color, sex) { 94 this.name = name; 95 this.breed = breed; 96 this.color = color; 97 this.sex = sex; 98 } 99} 100 101function testToString() { 102 let theDog = new Dog("Gabby", "Lab", "chocolate", "female"); 103 let res; 104 let start = ArkTools.timeInUs(); 105 for (let i = 0; i < 1_000_000; i++) { 106 res = theDog.toString(); 107 } 108 let end = ArkTools.timeInUs(); 109 let time = (end - start) / 1000 110 print(res); 111 print("Object ToString:\t"+String(time)+"\tms"); 112} 113 114testDefineProperty(); 115testHasOwnProperty(); 116testAssign(); 117testCreate(); 118testToString(); 119 120