1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16{ 17 const str = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?" 18 const time1 = Date.now() 19 for(var i = 0; i < 100000; ++i) { 20 str.replace(/dog/g, 'monkey'); 21 } 22 const time2 = Date.now() 23 const time3 = time2 - time1; 24 25 print("string replace regexp flag is g: " + time3.toString()); 26} 27 28{ 29 const re = /apples/gi; 30 const str = 'Apples are round, and apples are juicy.'; 31 const time1 = Date.now() 32 for(var i = 0; i < 100000; ++i) { 33 str.replace(re, 'Christmas'); 34 } 35 const time2 = Date.now() 36 const time3 = time2 - time1; 37 print("string replace regexp flag is gi :" + time3); 38} 39 40{ 41 function replacer(match, p1, p2, p3, offset, string) { 42 return [p1, p2, p3].join(' - '); 43 } 44 const time1 = Date.now() 45 for(var i = 0; i < 100000; ++i) { 46 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer); 47 } 48 const time2 = Date.now() 49 const time3 = time2 - time1; 50 print("string replace regexp pattern is /([^\d]*)(\d*)([^\w]*)/ : " + time3); 51} 52 53{ 54 const str = "Hello World. How are you doing?" 55 const time1 = Date.now() 56 for(var i = 0; i < 100000; ++i) { 57 str.split(" ", 3); 58 } 59 const time2 = Date.now() 60 const time3 = time2 - time1; 61 print("string split number of input parameters is 2 : " + time3); 62} 63 64{ 65 const str = "The quick brown fox jumps over the lazy dog." 66 const time1 = Date.now() 67 for(var i = 0; i < 100000; ++i) { 68 str.split(" "); 69 } 70 const time2 = Date.now() 71 const time3 = time2 - time1; 72 print("string split number of input parameters is 1 : " + time3); 73} 74 75{ 76 const str = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand " 77 const re = /\s*(?:;|$)\s*/; 78 const time1 = Date.now() 79 for(var i = 0; i < 100000; ++i) { 80 str.split(re); 81 } 82 const time2 = Date.now() 83 const time3 = time2 - time1; 84 print("string split into regexp.split : " + time3); 85} 86 87{ 88 const str = "The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?" 89 const searchTerm = 'dog'; 90 const time1 = Date.now() 91 for(var i = 0; i < 100000; ++i) { 92 str.indexOf(searchTerm); 93 } 94 const time2 = Date.now() 95 const time3 = time2 - time1; 96 print("string indexOf input parameter is a substring : " + time3); 97} 98 99{ 100 const str = "The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?" 101 const searchTerm = 'dDDog'; 102 const time1 = Date.now() 103 for(var i = 0; i < 100000; ++i) { 104 str.indexOf(searchTerm); 105 } 106 const time2 = Date.now() 107 const time3 = time2 - time1; 108 print("string indexOf Input parameter is not a substring : " + time3); 109} 110 111{ 112 const str = "The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?" 113 const time1 = Date.now() 114 for(var i = 0; i < 100000; ++i) { 115 str.indexOf("",10); 116 } 117 const time2 = Date.now() 118 const time3 = time2 - time1; 119 print("string indexOf number of input parameters is 2 : " + time3); 120} 121 122{ 123 const str = "Hello World. How are you doing?" 124 const time1 = Date.now() 125 for(var i = 0; i < 100000; ++i) { 126 str.slice(14); 127 } 128 const time2 = Date.now() 129 const time3 = time2 - time1; 130 print("string slice number of input parameters is 1 : " + time3); 131} 132 133{ 134 const str = "The quick brown fox jumps over the lazy dog." 135 const time1 = Date.now() 136 for(var i = 0; i < 100000; ++i) { 137 str.slice(4, 19); 138 } 139 const time2 = Date.now() 140 const time3 = time2 - time1; 141 print("string slice number of input parameters is 2 : " + time3); 142} 143 144{ 145 const str = "Hello World. How are you doing?" 146 const time1 = Date.now() 147 for(var i = 0; i < 100000; ++i) { 148 str.slice(-9, -5); 149 } 150 const time2 = Date.now() 151 const time3 = time2 - time1; 152 print("string slice input parameter is negative : " + time3); 153} 154 155{ 156 const str = "Hello World. How are you doing?" 157 const time1 = Date.now() 158 for(var i = 0; i < 100000; ++i) { 159 str.substring(1,10); 160 } 161 const time2 = Date.now() 162 const time3 = time2 - time1; 163 print("string substring the first parameter is less than the second parameter : " + time3); 164} 165 166{ 167 const str = "The quick brown fox jumps over the lazy dog." 168 const time1 = Date.now() 169 for(var i = 0; i < 100000; ++i) { 170 str.substring(19,3); 171 } 172 const time2 = Date.now() 173 const time3 = time2 - time1; 174 print("string substring the first parameter is greater than the second parameter : " + time3); 175} 176 177{ 178 const str = "Hello World. How are you doing?" 179 const time1 = Date.now() 180 for(var i = 0; i < 100000; ++i) { 181 str.substring(4,4); 182 } 183 const time2 = Date.now() 184 const time3 = time2 - time1; 185 print("string substring The first parameter is equal to the second parameter : " + time3); 186} 187 188{ 189 const str = "Hello World. How are you doing?" 190 const time1 = Date.now() 191 for(var i = 0; i < 100000; ++i) { 192 str.substr(1,2); 193 } 194 const time2 = Date.now() 195 const time3 = time2 - time1; 196 print("string substr the two parameters are adjacent numbers : " + time3); 197} 198 199{ 200 const str = 'abcdefghij' 201 const time1 = Date.now() 202 for(var i = 0; i < 100000; ++i) { 203 str.substr(-3,2); 204 } 205 const time2 = Date.now() 206 const time3 = time2 - time1; 207 print("string substr negative number in parameter : " + time3); 208} 209 210{ 211 const str = "abcdefghij" 212 const time1 = Date.now() 213 for(var i = 0; i < 100000; ++i) { 214 str.substr(1,7); 215 } 216 const time2 = Date.now() 217 const time3 = time2 - time1; 218 print("string substr number of input parameters is 2 : " + time3); 219} 220 221{ 222 const str = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?" 223 const time1 = Date.now() 224 for(var i = 0; i < 100000; ++i) { 225 str.replace('dog', 'monkey'); 226 } 227 const time2 = Date.now() 228 const time3 = time2 - time1; 229 print("string replace into string.replace searchtag in the middle of the input parameter : " + time3); 230} 231 232{ 233 const str = 'Twas the night before Xmas...' 234 const time1 = Date.now() 235 for(var i = 0; i < 100000; ++i) { 236 str.replace('Twas', 'Christmas'); 237 } 238 const time2 = Date.now() 239 const time3 = time2 - time1; 240 print("string replace into string.replace searchtag in the beginning of the input parameter : " + time3); 241} 242 243{ 244 const str = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?" 245 const time1 = Date.now() 246 for(var i = 0; i < 100000; ++i) { 247 str.replace('fox', 'monkey'); 248 } 249 const time2 = Date.now() 250 const time3 = time2 - time1; 251 print("string replace into string.repalce : " + time3); 252} 253 254{ 255 const str = "abc" 256 const time1 = Date.now() 257 for(var i = 0; i < 100000; ++i) { 258 str.repeat(3); 259 } 260 const time2 = Date.now() 261 const time3 = time2 - time1; 262 print("string repeat the parameter is integer : " + time3); 263} 264 265{ 266 const str = "abc" 267 const time1 = Date.now() 268 for(var i = 0; i < 100000; ++i) { 269 str.repeat(3.5); 270 } 271 const time2 = Date.now() 272 const time3 = time2 - time1; 273 print("string repeat the parameter is double : " + time3); 274} 275 276{ 277 const str = "abc" 278 const time1 = Date.now() 279 for(var i = 0; i < 100000; ++i) { 280 str.repeat(1); 281 } 282 const time2 = Date.now() 283 const time3 = time2 - time1; 284 print("string repeat the return value is himself : " + time3); 285} 286 287{ 288 const str = "Hello World. How are you doing?" 289 const time1 = Date.now() 290 for(var i = 0; i < 100000; ++i) { 291 str.startsWith('Hel'); 292 } 293 const time2 = Date.now() 294 const time3 = time2 - time1; 295 print("string startsWith return true : " + time3); 296} 297 298{ 299 const str = 'Saturday night plans'; 300 const time1 = Date.now() 301 for(var i = 0; i < 100000; ++i) { 302 str.startsWith('Sat'); 303 } 304 const time2 = Date.now() 305 const time3 = time2 - time1; 306 print("string startsWith return true : " + time3); 307} 308 309{ 310 const str = 'Saturday night plans'; 311 const time1 = Date.now() 312 for(var i = 0; i < 100000; ++i) { 313 str.startsWith('Sat', 3); 314 } 315 const time2 = Date.now() 316 const time3 = time2 - time1; 317 print("string startsWith return false : " + time3); 318} 319 320{ 321 const str = "Hello World. How are you doing?" 322 const time1 = Date.now() 323 for(var i = 0; i < 100000; ++i) { 324 str.charCodeAt(4); 325 } 326 const time2 = Date.now() 327 const time3 = time2 - time1; 328 print("string charCodeAt small input parameter value : " + time3); 329} 330 331{ 332 const str = "Hello World. How are you doing?" 333 const time1 = Date.now() 334 for(var i = 0; i < 100000; ++i) { 335 str.charCodeAt(10); 336 } 337 const time2 = Date.now() 338 const time3 = time2 - time1; 339 print("string charCodeAt bigger input parameter value : " + time3); 340} 341 342{ 343 const str = "Hello World. How are you doing?" 344 const time1 = Date.now() 345 for(var i = 0; i < 100000; ++i) { 346 str.charCodeAt(99); 347 } 348 const time2 = Date.now() 349 const time3 = time2 - time1; 350 print("string charCodeAt index out of range : " + time3); 351} 352 353{ 354 const str = "Hello World. How are you doing?" 355 const time1 = Date.now() 356 for(var i = 0; i < 100000; ++i) { 357 str.charAt(4); 358 } 359 const time2 = Date.now() 360 const time3 = time2 - time1; 361 print("string charAt small input parameter value : " + time3); 362} 363 364{ 365 const str = "Hello World. How are you doing?" 366 const time1 = Date.now() 367 for(var i = 0; i < 100000; ++i) { 368 str.charAt(10); 369 } 370 const time2 = Date.now() 371 const time3 = time2 - time1; 372 print("string charAt bigger input parameter value : " + time3); 373} 374 375{ 376 const str = "Hello World. How are you doing?" 377 const time1 = Date.now() 378 for(var i = 0; i < 100000; ++i) { 379 str.charAt(99); 380 } 381 const time2 = Date.now() 382 const time3 = time2 - time1; 383 print("string charAt index out of range : " + time3); 384} 385 386{ 387 const str = "Hello World. How are you doing?" 388 const time1 = Date.now() 389 for(var i = 0; i < 100000; ++i) { 390 str.toLowerCase(); 391 } 392 const time2 = Date.now() 393 const time3 = time2 - time1; 394 print("string toLowerCase Hello World. How are you doing? : " + time3); 395} 396 397{ 398 const str = 'The quick brown fox jumps over the lazy dog.' 399 const time1 = Date.now() 400 for(var i = 0; i < 100000; ++i) { 401 str.toLowerCase(); 402 } 403 const time2 = Date.now() 404 const time3 = time2 - time1; 405 print("string toLowerCase The quick brown fox jumps over the lazy dog : " + time3); 406} 407 408{ 409 const str = "Hello World" 410 const time1 = Date.now() 411 for(var i = 0; i < 100000; ++i) { 412 str.toLowerCase(); 413 } 414 const time2 = Date.now() 415 const time3 = time2 - time1; 416 print("string toLowerCase Hello World : " + time3); 417} 418 419{ 420 const str = "Hello World. How are you doing?" 421 const time1 = Date.now() 422 for(var i = 0; i < 100000; ++i) { 423 str.toUpperCase(); 424 } 425 const time2 = Date.now() 426 const time3 = time2 - time1; 427 print("string toUpperCase Hello World. How are you doing? : " + time3); 428} 429 430{ 431 const str = "Hello World" 432 const time1 = Date.now() 433 for(var i = 0; i < 100000; ++i) { 434 str.toUpperCase(); 435 } 436 const time2 = Date.now() 437 const time3 = time2 - time1; 438 print("string toUpperCase Hello World : " + time3); 439} 440 441{ 442 const str = 'The quick brown fox jumps over the lazy dog.' 443 const time1 = Date.now() 444 for(var i = 0; i < 100000; ++i) { 445 str.toUpperCase(); 446 } 447 const time2 = Date.now() 448 const time3 = time2 - time1; 449 print("string toUpperCase The quick brown fox jumps over the lazy dog. : " + time3); 450} 451 452{ 453 const time1 = Date.now() 454 for(var i = 0; i < 100000; ++i) { 455 'a'.localeCompare('c'); 456 } 457 const time2 = Date.now() 458 const time3 = time2 - time1; 459 print("string localeCompare letter a is before c yielding a negative value : " + time3); 460} 461 462{ 463 const time1 = Date.now() 464 for(var i = 0; i < 100000; ++i) { 465 'check'.localeCompare('against'); 466 } 467 const time2 = Date.now() 468 const time3 = time2 - time1; 469 print("string localeCompare Alphabetically the word check after against yielding a positive value : " + time3); 470} 471 472{ 473 const time1 = Date.now() 474 for(var i = 0; i < 100000; ++i) { 475 'a'.localeCompare('a'); 476 } 477 const time2 = Date.now() 478 const time3 = time2 - time1; 479 print("string localeCompare a and a are equivalent yielding a neutral value of zero : " + time3); 480} 481 482{ 483 const str = " This is a test string to test the interface " 484 const time1 = Date.now() 485 for(var i = 0; i < 100000; ++i) { 486 str.trim(); 487 } 488 const time2 = Date.now() 489 const time3 = time2 - time1; 490 print(" This is a test string to test the interface string trim : " + time3); 491} 492 493{ 494 const str = ' Hello world! ' 495 const time1 = Date.now() 496 for(var i = 0; i < 100000; ++i) { 497 str.trim(); 498 } 499 const time2 = Date.now() 500 const time3 = time2 - time1; 501 print(" Hello world! string trim : " + time3); 502} 503 504{ 505 const str = " This is a test string " 506 const time1 = Date.now() 507 for(var i = 0; i < 100000; ++i) { 508 str.trim(); 509 } 510 const time2 = Date.now() 511 const time3 = time2 - time1; 512 print(" This is a test string string trim : " + time3); 513}