1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * sanity check that apply to call specialization is faster than apply.
  26  *
  27  * @test
  28   * @run
  29  */
  30 
  31 var Class = {
  32     create: function() {
  33     return function() { //vararg
  34         this.initialize.apply(this, arguments);
  35     }
  36     }
  37 };
  38 
  39 Color = Class.create();
  40 Color.prototype = {
  41     red: 0, green: 0, blue: 0,
  42     initialize: function(r,g,b) {
  43     this.red = r;
  44     this.green = g;
  45     this.blue = b;
  46     }
  47 };
  48 
  49 var time1 = 0;
  50 var time2 = 0;
  51 
  52 function set_time1(t) {
  53     time1 = t;
  54 }
  55 
  56 function set_time2(t) {
  57     time2 = t;
  58 }
  59 
  60 function bench(x, set_time) {
  61     var d = new Date;
  62     var colors = new Array(16);
  63     for (var i=0;i<1e8;i++) {
  64     colors[i & 0xf] = new Color(1,2,3);
  65     }
  66     var t = new Date - d;
  67     set_time(t);
  68     return colors;
  69 }
  70 
  71 //warmup
  72 print("Running warmup");
  73 bench(17, set_time1);
  74 
  75 print("Running sharp run");
  76 bench(17, set_time1);
  77 
  78 print("Swapping out call");
  79 Function.prototype.call = function() {
  80     throw "This should not happen, apply should be called instead";
  81 };
  82 
  83 print("Rerunning invalidated");
  84 bench(17, set_time2);
  85 
  86 print("All done!");
  87 
  88 if (time1 > time2) {
  89     print("ERROR: time1 > time2 (" + time1 + " > " + time2 + ")");
  90 } else {
  91     print("Times OK");
  92 }
  93