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 testSet() { 22 let key = "key"; 23 let myMap = new Map(); 24 let start = ArkTools.timeInUs(); 25 for (let i = 0; i < 1_000_000; i++) { 26 myMap.set(key, i); 27 } 28 let end = ArkTools.timeInUs(); 29 let time = (end - start) / 1000 30 print("Map Set:\t" + String(time) + "\tms"); 31} 32 33function testClear() { 34 let myMap = new Map(); 35 let start = ArkTools.timeInUs(); 36 for (let i = 0; i < 1_000_000; i++) { 37 myMap.clear(); 38 } 39 let end = ArkTools.timeInUs(); 40 let time = (end - start) / 1000 41 print("Map Clear:\t" + String(time) + "\tms"); 42} 43 44function testDelete() { 45 let myMap = new Map(); 46 let key = "key"; 47 myMap.set(key, key); 48 let start = ArkTools.timeInUs(); 49 for (let i = 0; i < 1_000_000; i++) { 50 myMap.delete(key); 51 } 52 let end = ArkTools.timeInUs(); 53 let time = (end - start) / 1000 54 print("Map Delete:\t" + String(time) + "\tms"); 55} 56 57function testHas() { 58 let myMap = new Map(); 59 let key = "key"; 60 myMap.set(key, key); 61 let start = ArkTools.timeInUs(); 62 for (let i = 0; i < 1_000_000; i++) { 63 myMap.has(key); 64 } 65 let end = ArkTools.timeInUs(); 66 let time = (end - start) / 1000 67 print("Map Has:\t" + String(time) + "\tms"); 68} 69 70function setElements(value, key, map) {} 71function testForEach() { 72 let myMap = new Map(); 73 let key = "key"; 74 myMap.set(key, key); 75 let start = ArkTools.timeInUs(); 76 for (let i = 0; i < 1_000_000; i++) { 77 myMap.forEach(setElements); 78 } 79 let end = ArkTools.timeInUs(); 80 let time = (end - start) / 1000 81 print("Map ForEach:\t" + String(time) + "\tms"); 82} 83 84function testKeys() { 85 let myMap = new Map(); 86 let start = ArkTools.timeInUs(); 87 for (let i = 0; i < 1_000_000; i++) { 88 myMap.keys(); 89 } 90 let end = ArkTools.timeInUs(); 91 let time = (end - start) / 1000 92 print("Map Keys:\t" + String(time) + "\tms"); 93} 94 95function testValues() { 96 let myMap = new Map(); 97 let start = ArkTools.timeInUs(); 98 for (let i = 0; i < 1_000_000; i++) { 99 myMap.values(); 100 } 101 let end = ArkTools.timeInUs(); 102 let time = (end - start) / 1000 103 print("Map Values:\t" + String(time) + "\tms"); 104} 105 106function testEntries() { 107 let myMap = new Map(); 108 let start = ArkTools.timeInUs(); 109 for (let i = 0; i < 1_000_000; i++) { 110 myMap.entries(); 111 } 112 let end = ArkTools.timeInUs(); 113 let time = (end - start) / 1000 114 print("Map Entries:\t" + String(time) + "\tms"); 115} 116 117testSet(); 118testClear(); 119testDelete(); 120testHas(); 121testForEach(); 122testKeys(); 123testValues(); 124testEntries(); 125