nashorn/test/examples/array-micro.js
author cl
Thu, 18 Jul 2013 03:39:39 -0700
changeset 18837 d0545441225e
parent 18318 5e4244619d79
child 21438 4292865c758b
permissions -rw-r--r--
Added tag jdk8-b99 for changeset d104c72ebc8a
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.
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
     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
     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:
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
     7
 * 
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.
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
    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
    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.
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
    14
 * 
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.
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
    18
 * 
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
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
    93
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
    94
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
    95
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
    96
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
    97
    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
    98
});
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
    99
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
   100
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
   101
    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
   102
});
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
   103
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
   104
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
   105
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
   106
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
   107
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
   108
    [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
   109
});
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
   110
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
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
   112
    [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
   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