Home | History | Annotate | Download | only in SpreadCalls
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 new BenchmarkSuite('Call', [1000], [
      6   new Benchmark('Call-Sum', false, false, 0,
      7                 CallSum, CallSumSetup,
      8                 CallSumTearDown),
      9 ]);
     10 
     11 new BenchmarkSuite('CallMethod', [1000], [
     12   new Benchmark('CallMethod-Sum', false, false, 0,
     13                 CallMethodSum, CallSumSetup, CallMethodSumTearDown),
     14 ]);
     15 
     16 new BenchmarkSuite('CallNew', [1000], [
     17   new Benchmark('CallNew-Sum', false, false, 0,
     18                 CallNewSum, CallSumSetup,
     19                 CallNewSumTearDown),
     20 ]);
     21 
     22 var result;
     23 var objectToSpread;
     24 
     25 function sum() {
     26   var result = arguments[0];
     27   for (var i = 1; i < arguments.length; ++i) {
     28     result += arguments[i];
     29   }
     30   return result;
     31 }
     32 
     33 function CallSumSetup() {
     34   result = undefined;
     35   objectToSpread = [];
     36   for (var i = 0; i < 100; ++i) objectToSpread.push(i + 1);
     37 }
     38 
     39 function CallSum() {
     40   result = sum(...objectToSpread);
     41 }
     42 
     43 function CallSumTearDown() {
     44   var expected = 100 * (100 + 1) / 2;
     45   return result === expected;
     46 }
     47 
     48 // ----------------------------------------------------------------------------
     49 
     50 var O = { sum: sum };
     51 function CallMethodSum() {
     52   result = O.sum(...objectToSpread);
     53 }
     54 
     55 function CallMethodSumTearDown() {
     56   var expected = 100 * (100 + 1) / 2;
     57   return result === expected;
     58 }
     59 
     60 // ----------------------------------------------------------------------------
     61 
     62 function Sum() {
     63   var result = arguments[0];
     64   for (var i = 1; i < arguments.length; ++i) {
     65     result += arguments[i];
     66   }
     67   return this.sum = result;
     68 }
     69 
     70 function CallNewSum() {
     71   result = new Sum(...objectToSpread);
     72 }
     73 
     74 function CallNewSumTearDown() {
     75   var expected = 100 * (100 + 1) / 2;
     76   return result instanceof Sum && result.sum === expected;
     77 }
     78