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 5new BenchmarkSuite('Call', [1000], [ 6 new Benchmark('Call-Sum', false, false, 0, 7 CallSum, CallSumSetup, 8 CallSumTearDown), 9]); 10 11new BenchmarkSuite('CallMethod', [1000], [ 12 new Benchmark('CallMethod-Sum', false, false, 0, 13 CallMethodSum, CallSumSetup, CallMethodSumTearDown), 14]); 15 16new BenchmarkSuite('CallNew', [1000], [ 17 new Benchmark('CallNew-Sum', false, false, 0, 18 CallNewSum, CallSumSetup, 19 CallNewSumTearDown), 20]); 21 22var result; 23var objectToSpread; 24 25function 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 33function CallSumSetup() { 34 result = undefined; 35 objectToSpread = []; 36 for (var i = 0; i < 100; ++i) objectToSpread.push(i + 1); 37} 38 39function CallSum() { 40 result = sum(...objectToSpread); 41} 42 43function CallSumTearDown() { 44 var expected = 100 * (100 + 1) / 2; 45 return result === expected; 46} 47 48// ---------------------------------------------------------------------------- 49 50var O = { sum: sum }; 51function CallMethodSum() { 52 result = O.sum(...objectToSpread); 53} 54 55function CallMethodSumTearDown() { 56 var expected = 100 * (100 + 1) / 2; 57 return result === expected; 58} 59 60// ---------------------------------------------------------------------------- 61 62function 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 70function CallNewSum() { 71 result = new Sum(...objectToSpread); 72} 73 74function CallNewSumTearDown() { 75 var expected = 100 * (100 + 1) / 2; 76 return result instanceof Sum && result.sum === expected; 77} 78