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