1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * JDK-8068513: Adding elements to a javascript 'object' (a map) is slow 26 * 27 * @test 28 * @run 29 */ 30 31 var map = {}; 32 var keys = []; 33 var values = []; 34 35 for (i = 0; i < 5000; i++) { 36 var key = 'key' + i; 37 var value = i; 38 keys.push(key); 39 values.push(value); 40 map[key] = value; 41 } 42 43 function testAssertions() { 44 Assert.assertTrue(Object.keys(map).length === values.length); 45 46 var c = 0; 47 for (var k in map) { 48 Assert.assertTrue(k === keys[c]); 49 Assert.assertTrue(map[k] === values[c]); 50 c++; 51 } 52 53 Assert.assertTrue(c === values.length); 54 } 55 56 // redefine existing property 57 Object.defineProperty(map, "key2000", { enumerable: true, get: function() { return 'new value 2000' } }); 58 values[2000] = 'new value 2000'; 59 60 testAssertions(); 61 62 // define new property 63 Object.defineProperty(map, "defined property", { enumerable: true, configurable: true, get: function() { return 13 } }); 64 keys.push('defined property'); 65 values.push(13); 66 67 testAssertions(); 68 69 // delete and redefine 70 delete map.key3000; 71 map.key3000 = 'new value'; 72 keys.splice(3000, 1); 73 values.splice(3000, 1); 74 keys.push('key3000'); 75 values.push('new value'); 76 77 testAssertions(); 78 79 // delete all properties 80 while (values.length > 0) { 81 values.pop(); 82 delete map[keys.pop()]; 83 } 84 85 testAssertions(); 86 87 // add a few new ones 88 for (var i = 0; i < 1000; i++) { 89 keys.push('k' + i); 90 values.push('v' + i); 91 map['k' + i] = 'v' + i; 92 } 93 94 testAssertions(); 95