test/nashorn/examples/array-micro.js
author alanb
Sat, 30 Nov 2019 16:21:19 +0000
changeset 59329 289000934908
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation Reviewed-by: dfuchs, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
     1
/*
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 21438
diff changeset
     3
 *
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
     6
 * are met:
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 21438
diff changeset
     7
 *
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 21438
diff changeset
    10
 *
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 21438
diff changeset
    14
 *
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 21438
diff changeset
    18
 *
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    30
 */
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    31
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    32
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    33
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    34
function bench(name, func) {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    35
    var start = Date.now();
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    36
    for (var iter = 0; iter < 5e6; iter++) {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    37
        func();
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    38
    }
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    39
    print((Date.now() - start) + "\t" + name);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    40
}
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    41
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    42
bench("[]", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    43
    [];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    44
    [];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    45
    [];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    46
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    47
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    48
bench("[1, 2, 3]", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    49
    [1, 2, 3];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    50
    [1, 2, 3];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    51
    [1, 2, 3];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    52
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    53
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    54
bench("[1 .. 20]", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    55
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    56
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    57
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    58
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    59
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    60
bench("new Array()", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    61
    new Array();
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    62
    new Array();
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    63
    new Array();
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    64
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    65
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    66
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    67
bench("new Array(1, 2, 3)", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    68
    new Array(1, 2, 3);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    69
    new Array(1, 2, 3);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    70
    new Array(1, 2, 3);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    71
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    72
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    73
bench("new Array(10)", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    74
    new Array(10);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    75
    new Array(10);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    76
    new Array(10);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    77
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    78
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    79
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    80
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    81
bench("get", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    82
    array[0];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    83
    array[3];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    84
    array[6];
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    85
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    86
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    87
bench("set", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    88
    array[0] = 0;
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    89
    array[3] = 3;
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    90
    array[6] = 6;
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    91
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
    92
21438
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
    93
bench("push", function() {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
    94
    var arr = [1, 2, 3];
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
    95
    arr.push(4);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
    96
    arr.push(5);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
    97
    arr.push(6);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
    98
});
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
    99
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   100
bench("pop", function() {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   101
    var arr = [1, 2, 3];
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   102
    arr.pop();
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   103
    arr.pop();
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   104
    arr.pop();
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   105
});
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   106
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   107
bench("splice", function() {
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   108
    [1, 2, 3].splice(0, 2, 5, 6, 7);
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   109
});
4292865c758b 8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents: 18318
diff changeset
   110
18318
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   111
var all = function(e) { return true; };
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   112
var none = function(e) { return false; };
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   113
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   114
bench("filter all", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   115
    array.filter(all);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   116
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   117
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   118
bench("filter none", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   119
    array.filter(none);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   120
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   121
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   122
var up = function(a, b) { return a > b ? 1 : -1; };
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   123
var down = function(a, b) { return a < b ? 1 : -1; };
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   124
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   125
bench("sort up", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   126
    [1, 2, 3, 4].sort(up);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   127
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   128
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   129
bench("sort down", function() {
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   130
    [1, 2, 3, 4].sort(down);
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   131
});
5e4244619d79 8012291: NativeArray is inconsistent in using long for length and index in some places and int for the same in other places
hannesw
parents:
diff changeset
   132