|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import {createBenchmark} from '../micro_bench'; |
| 9 | + |
| 10 | +// These benchmarks compare various implementations of the `renderStringify` utility |
| 11 | +// which vary in subtle ways which end up having an effect on performance. |
| 12 | + |
| 13 | +/** Uses string concatenation to convert a value into a string. */ |
| 14 | +function renderStringifyConcat(value: any): string { |
| 15 | + if (typeof value === 'string') return value; |
| 16 | + if (value == null) return ''; |
| 17 | + return '' + value; |
| 18 | +} |
| 19 | + |
| 20 | +/** Uses `toString` to convert a value into a string. */ |
| 21 | +function renderStringifyToString(value: any): string { |
| 22 | + if (typeof value === 'string') return value; |
| 23 | + if (value == null) return ''; |
| 24 | + return value.toString(); |
| 25 | +} |
| 26 | + |
| 27 | +/** Uses the `String` constructor to convert a value into a string. */ |
| 28 | +function renderStringifyConstructor(value: any): string { |
| 29 | + if (typeof value === 'string') return value; |
| 30 | + if (value == null) return ''; |
| 31 | + return String(value); |
| 32 | +} |
| 33 | + |
| 34 | +const objects: any[] = []; |
| 35 | +const objectsWithToString: any[] = []; |
| 36 | + |
| 37 | +// Allocate a bunch of objects with a unique structure. |
| 38 | +for (let i = 0; i < 1000000; i++) { |
| 39 | + objects.push({['foo_' + i]: i}); |
| 40 | + objectsWithToString.push({['foo_' + i]: i, toString: () => 'x'}); |
| 41 | +} |
| 42 | +const max = objects.length - 1; |
| 43 | +let i = 0; |
| 44 | + |
| 45 | +const benchmarkRefresh = createBenchmark('renderStringify'); |
| 46 | +const renderStringifyConcatTime = benchmarkRefresh('concat'); |
| 47 | +const renderStringifyConcatWithToStringTime = benchmarkRefresh('concat with toString'); |
| 48 | +const renderStringifyToStringTime = benchmarkRefresh('toString'); |
| 49 | +const renderStringifyToStringWithToStringTime = benchmarkRefresh('toString with toString'); |
| 50 | +const renderStringifyConstructorTime = benchmarkRefresh('constructor'); |
| 51 | +const renderStringifyConstructorWithToStringTime = benchmarkRefresh('constructor with toString'); |
| 52 | +const renderStringifyToStringMonoTime = benchmarkRefresh('toString mono'); |
| 53 | +const renderStringifyToStringWithToStringMonoTime = benchmarkRefresh('toString with toString mono'); |
| 54 | + |
| 55 | +// Important! This code is somewhat repetitive, but we can't move it out into something like |
| 56 | +// `benchmark(name, stringifyFn)`, because passing in the function as a parameter breaks inlining. |
| 57 | + |
| 58 | +// String concatenation |
| 59 | +while (renderStringifyConcatTime()) { |
| 60 | + renderStringifyConcat(objects[i]); |
| 61 | + i = i < max ? i + 1 : 0; |
| 62 | +} |
| 63 | + |
| 64 | +while (renderStringifyConcatWithToStringTime()) { |
| 65 | + renderStringifyConcat(objectsWithToString[i]); |
| 66 | + i = i < max ? i + 1 : 0; |
| 67 | +} |
| 68 | +///////////// |
| 69 | + |
| 70 | +// String() |
| 71 | +while (renderStringifyConstructorTime()) { |
| 72 | + renderStringifyConstructor(objects[i]); |
| 73 | + i = i < max ? i + 1 : 0; |
| 74 | +} |
| 75 | + |
| 76 | +while (renderStringifyConstructorWithToStringTime()) { |
| 77 | + renderStringifyConstructor(objectsWithToString[i]); |
| 78 | + i = i < max ? i + 1 : 0; |
| 79 | +} |
| 80 | +///////////// |
| 81 | + |
| 82 | +// toString |
| 83 | +while (renderStringifyToStringTime()) { |
| 84 | + renderStringifyToString(objects[i]); |
| 85 | + i = i < max ? i + 1 : 0; |
| 86 | +} |
| 87 | + |
| 88 | +while (renderStringifyToStringWithToStringTime()) { |
| 89 | + renderStringifyToString(objectsWithToString[i]); |
| 90 | + i = i < max ? i + 1 : 0; |
| 91 | +} |
| 92 | +///////////// |
| 93 | + |
| 94 | +// toString mono |
| 95 | +while (renderStringifyToStringMonoTime()) { |
| 96 | + renderStringifyToString(objects[0]); |
| 97 | +} |
| 98 | + |
| 99 | +while (renderStringifyToStringWithToStringMonoTime()) { |
| 100 | + renderStringifyToString(objectsWithToString[0]); |
| 101 | +} |
| 102 | +///////////// |
| 103 | + |
| 104 | +benchmarkRefresh.report(); |
0 commit comments