test/examples/string-micro.js

Mon, 28 Jul 2014 07:23:56 -0700

author
tbell
date
Mon, 28 Jul 2014 07:23:56 -0700
changeset 910
552ee417f276
parent 0
b1a7da25b547
child 962
ac62e33a99b0
permissions
-rw-r--r--

Added tag jdk8u20-b25 for changeset d3da140e1793

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 *
aoqi@0 4 * Redistribution and use in source and binary forms, with or without
aoqi@0 5 * modification, are permitted provided that the following conditions
aoqi@0 6 * are met:
aoqi@0 7 *
aoqi@0 8 * - Redistributions of source code must retain the above copyright
aoqi@0 9 * notice, this list of conditions and the following disclaimer.
aoqi@0 10 *
aoqi@0 11 * - Redistributions in binary form must reproduce the above copyright
aoqi@0 12 * notice, this list of conditions and the following disclaimer in the
aoqi@0 13 * documentation and/or other materials provided with the distribution.
aoqi@0 14 *
aoqi@0 15 * - Neither the name of Oracle nor the names of its
aoqi@0 16 * contributors may be used to endorse or promote products derived
aoqi@0 17 * from this software without specific prior written permission.
aoqi@0 18 *
aoqi@0 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
aoqi@0 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
aoqi@0 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
aoqi@0 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
aoqi@0 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
aoqi@0 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
aoqi@0 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
aoqi@0 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
aoqi@0 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
aoqi@0 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
aoqi@0 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
aoqi@0 30 */
aoqi@0 31
aoqi@0 32
aoqi@0 33 var str = "The quick gray nashorn jumps over the lazy zebra.";
aoqi@0 34
aoqi@0 35 function bench(name, func) {
aoqi@0 36 var start = Date.now();
aoqi@0 37 for (var iter = 0; iter < 5000000; iter++) {
aoqi@0 38 func();
aoqi@0 39 }
aoqi@0 40 print((Date.now() - start) + "\t" + name);
aoqi@0 41 }
aoqi@0 42
aoqi@0 43 bench("[]", function() {
aoqi@0 44 str[0];
aoqi@0 45 str[1];
aoqi@0 46 str[2];
aoqi@0 47 });
aoqi@0 48
aoqi@0 49 bench("fromCharCode", function() {
aoqi@0 50 String.fromCharCode(97);
aoqi@0 51 String.fromCharCode(98);
aoqi@0 52 String.fromCharCode(99);
aoqi@0 53 });
aoqi@0 54
aoqi@0 55 bench("charAt 1", function() {
aoqi@0 56 str.charAt(0);
aoqi@0 57 str.charAt(1);
aoqi@0 58 str.charAt(2);
aoqi@0 59 });
aoqi@0 60
aoqi@0 61 bench("charAt 2", function() {
aoqi@0 62 str.charAt(100);
aoqi@0 63 str.charAt(-1);
aoqi@0 64 });
aoqi@0 65
aoqi@0 66 bench("charCodeAt 1", function() {
aoqi@0 67 str.charCodeAt(0);
aoqi@0 68 str.charCodeAt(1);
aoqi@0 69 str.charCodeAt(2);
aoqi@0 70 });
aoqi@0 71
aoqi@0 72 bench("charCodeAt 2", function() {
aoqi@0 73 str.charCodeAt(100);
aoqi@0 74 str.charCodeAt(-1);
aoqi@0 75 });
aoqi@0 76
aoqi@0 77 bench("indexOf 1", function() {
aoqi@0 78 str.indexOf("T");
aoqi@0 79 str.indexOf("h");
aoqi@0 80 str.indexOf("e");
aoqi@0 81 });
aoqi@0 82
aoqi@0 83 bench("indexOf 2", function() {
aoqi@0 84 str.indexOf("T", 0);
aoqi@0 85 str.indexOf("h", 1);
aoqi@0 86 str.indexOf("e", 2);
aoqi@0 87 });
aoqi@0 88
aoqi@0 89 bench("lastIndexOf", function() {
aoqi@0 90 str.indexOf("a");
aoqi@0 91 str.indexOf("r");
aoqi@0 92 str.indexOf("b");
aoqi@0 93 });
aoqi@0 94
aoqi@0 95 bench("slice", function() {
aoqi@0 96 str.slice(5);
aoqi@0 97 str.slice(5);
aoqi@0 98 str.slice(5);
aoqi@0 99 });
aoqi@0 100
aoqi@0 101 bench("split 1", function() {
aoqi@0 102 str.split();
aoqi@0 103 });
aoqi@0 104
aoqi@0 105 bench("split 2", function() {
aoqi@0 106 str.split("foo");
aoqi@0 107 });
aoqi@0 108
aoqi@0 109 bench("split 3", function() {
aoqi@0 110 str.split(/foo/);
aoqi@0 111 });
aoqi@0 112
aoqi@0 113 bench("substring 1", function() {
aoqi@0 114 str.substring(0);
aoqi@0 115 str.substring(0);
aoqi@0 116 str.substring(0);
aoqi@0 117 });
aoqi@0 118
aoqi@0 119 bench("substring 2", function() {
aoqi@0 120 str.substring(0, 5);
aoqi@0 121 str.substring(0, 5);
aoqi@0 122 str.substring(0, 5);
aoqi@0 123 });
aoqi@0 124
aoqi@0 125 bench("substr", function() {
aoqi@0 126 str.substr(0);
aoqi@0 127 str.substr(0);
aoqi@0 128 str.substr(0);
aoqi@0 129 });
aoqi@0 130
aoqi@0 131 bench("slice", function() {
aoqi@0 132 str.slice(0);
aoqi@0 133 str.slice(0);
aoqi@0 134 str.slice(0);
aoqi@0 135 });
aoqi@0 136
aoqi@0 137 bench("concat", function() {
aoqi@0 138 str.concat(str);
aoqi@0 139 str.concat(str);
aoqi@0 140 str.concat(str);
aoqi@0 141 });
aoqi@0 142
aoqi@0 143 bench("trim", function() {
aoqi@0 144 str.trim();
aoqi@0 145 str.trim();
aoqi@0 146 str.trim();
aoqi@0 147 });
aoqi@0 148
aoqi@0 149 bench("toUpperCase", function() {
aoqi@0 150 str.toUpperCase();
aoqi@0 151 });
aoqi@0 152
aoqi@0 153 bench("toLowerCase", function() {
aoqi@0 154 str.toLowerCase();
aoqi@0 155 });
aoqi@0 156
aoqi@0 157 bench("valueOf", function() {
aoqi@0 158 str.valueOf();
aoqi@0 159 str.valueOf();
aoqi@0 160 str.valueOf();
aoqi@0 161 });
aoqi@0 162
aoqi@0 163 bench("toString", function() {
aoqi@0 164 str.toString();
aoqi@0 165 str.toString();
aoqi@0 166 str.toString();
aoqi@0 167 });
aoqi@0 168
aoqi@0 169 bench("String", function() {
aoqi@0 170 String(str);
aoqi@0 171 String(str);
aoqi@0 172 String(str);
aoqi@0 173 });
aoqi@0 174
aoqi@0 175 bench("new String", function() {
aoqi@0 176 new String(str);
aoqi@0 177 new String(str);
aoqi@0 178 new String(str);
aoqi@0 179 });

mercurial