Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 1 | // Copyright 2016 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 | |
| 6 | new BenchmarkSuite('Generators', [1000], [ |
| 7 | new Benchmark('Basic', false, false, 0, Basic), |
| 8 | new Benchmark('Loop', false, false, 0, Loop), |
| 9 | new Benchmark('Input', false, false, 0, Input), |
| 10 | new Benchmark('YieldStar', false, false, 0, YieldStar), |
| 11 | ]); |
| 12 | |
| 13 | |
| 14 | // ---------------------------------------------------------------------------- |
| 15 | // Benchmark: Basic |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | |
| 18 | function* five() { |
| 19 | yield 1; |
| 20 | yield 2; |
| 21 | yield 3; |
| 22 | yield 4; |
| 23 | yield 5; |
| 24 | } |
| 25 | |
| 26 | function Basic() { |
| 27 | let g = five(); |
| 28 | let sum = 0; |
| 29 | sum += g.next().value; |
| 30 | sum += g.next().value; |
| 31 | sum += g.next().value; |
| 32 | sum += g.next().value; |
| 33 | sum += g.next().value; |
| 34 | if (sum != 15 || !g.next().done) throw "wrong"; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | // Benchmark: Loop |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | function* fibonacci() { |
| 43 | let x = 0; |
| 44 | let y = 1; |
| 45 | yield x; |
| 46 | while (true) { |
| 47 | yield y; |
| 48 | let tmp = x; |
| 49 | x = y; |
| 50 | y += tmp; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | function Loop() { |
| 55 | let n = 0; |
| 56 | let x; |
| 57 | for (x of fibonacci()) { |
| 58 | if (++n === 42) break; |
| 59 | } |
| 60 | if (x != 165580141) throw "wrong"; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | // ---------------------------------------------------------------------------- |
| 66 | // Benchmark: Input |
| 67 | // ---------------------------------------------------------------------------- |
| 68 | |
| 69 | function* multiples(x) { |
| 70 | let skip = function.sent || 0; |
| 71 | let next = 0; |
| 72 | while (true) { |
| 73 | if (skip === 0) { |
| 74 | skip = yield next; |
| 75 | } else { |
| 76 | skip--; |
| 77 | } |
| 78 | next += x; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | function Input() { |
| 83 | let g = multiples(3); |
| 84 | results = [g.next(2), g.next(0), g.next(5), g.next(10)]; |
| 85 | if (results.slice(-1)[0].value != 60) throw "wrong"; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | // ---------------------------------------------------------------------------- |
| 90 | // Benchmark: YieldStar |
| 91 | // ---------------------------------------------------------------------------- |
| 92 | |
| 93 | function* infix(node) { |
| 94 | if (node) { |
| 95 | yield* infix(node.left); |
| 96 | yield node.label; |
| 97 | yield* infix(node.right); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | class Node { |
| 102 | constructor(label, left, right) { |
| 103 | this.label = label; |
| 104 | this.left = left; |
| 105 | this.right = right; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | function YieldStar() { |
| 110 | let tree = new Node(1, |
| 111 | new Node(2, |
| 112 | new Node(3, |
| 113 | new Node(4, |
| 114 | new Node(16, |
| 115 | new Node(5, |
| 116 | new Node(23, |
| 117 | new Node(0), |
| 118 | new Node(17)), |
| 119 | new Node(44, new Node(20)))), |
| 120 | new Node(7, |
| 121 | undefined, |
| 122 | new Node(23, |
| 123 | new Node(0), |
| 124 | new Node(41, undefined, new Node(11))))), |
| 125 | new Node(8)), |
| 126 | new Node(5)), |
| 127 | new Node(6, undefined, new Node(7))); |
| 128 | let labels = [...(infix(tree))]; |
| 129 | // 0,23,17,5,20,44,16,4,7,0,23,41,11,3,8,2,5,1,6,7 |
| 130 | if (labels[0] != 0) throw "wrong"; |
| 131 | } |