angular-cn/packages/core/test/render3/perf
Kristiyan Kostadinov 11cd37fae3 fix(core): not invoking object's toString when rendering to the DOM (#39843)
Currently we convert objects to strings using `'' + value` which is quickest,
but it stringifies the value using its `valueOf`, rather than `toString`. These
changes switch to using `String(value)` which has identical performance
and calls the `toString` method as expected. Note that another option
was calling `toString` directly, but benchmarking showed it to be slower.

I've included the benchmark I used to verify the performance so we have it
for future reference and we can reuse it when making changes to `renderStringify`
in the future.

Also for reference, here are the results of the benchmark:

```
Benchmark: renderStringify
 concat: 2.006 ns(0%)
 concat with toString: 2.201 ns(-10%)
 toString: 237.494 ns(-11741%)
 toString with toString: 121.072 ns(-5937%)
 constructor: 2.201 ns(-10%)
 constructor with toString: 2.201 ns(-10%)
 toString mono: 14.536 ns(-625%)
 toString with toString mono: 9.757 ns(-386%)
```

Fixes #38839.

PR Close #39843
2020-11-30 15:49:57 -08:00
..
class_binding refactor(core): use relative import paths in micro benchmarks (#39142) 2020-10-14 14:10:13 -07:00
directive_inputs build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
directive_instantiate refactor(core): use relative import paths in micro benchmarks (#39142) 2020-10-14 14:10:13 -07:00
duplicate_map_based_style_and_class_bindings build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
duplicate_style_and_class_bindings build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
element_text_create refactor(core): Remove `TViewNode` as it is no longer used. (#38707) 2020-09-28 16:15:59 -04:00
host_binding build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
i18n test(core): add micro benchmarks for i18n scenarios (#39142) 2020-10-14 14:10:13 -07:00
interpolation build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
listeners refactor(core): Remove `TViewNode` as it is no longer used. (#38707) 2020-09-28 16:15:59 -04:00
map_based_style_and_class_bindings build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
ng_template refactor(core): Cleanup circular dependency between ViewEngine and Ivy `ViewContainerRef`. (#39621) 2020-11-16 09:12:46 -08:00
noop_change_detection build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
property_binding build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
property_binding_update build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
render_stringify fix(core): not invoking object's toString when rendering to the DOM (#39843) 2020-11-30 15:49:57 -08:00
style_and_class_bindings build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
style_binding build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
view_destroy_hook refactor(core): use relative import paths in micro benchmarks (#39142) 2020-10-14 14:10:13 -07:00
BUILD.bazel fix(core): not invoking object's toString when rendering to the DOM (#39843) 2020-11-30 15:49:57 -08:00
README.md refactor(dev-infra): ng_rollup_bundle rule should leverage `@bazel/rollup` (#37623) 2020-06-22 10:55:28 -07:00
micro_bench.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
noop_renderer.ts refactor(core): clean up circular dependencies (#39722) 2020-11-18 09:15:29 -08:00
noop_renderer_spec.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
profile_all.js refactor(dev-infra): ng_rollup_bundle rule should leverage `@bazel/rollup` (#37623) 2020-06-22 10:55:28 -07:00
profile_in_browser.html test(core): add micro benchmarks for i18n scenarios (#39142) 2020-10-14 14:10:13 -07:00
setup.ts refactor(core): Remove `TViewNode` as it is no longer used. (#38707) 2020-09-28 16:15:59 -04:00
shared.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00

README.md

Build

yarn bazel build //packages/core/test/render3/perf:${BENCHMARK}_lib.min_debug.js --config=ivy

Run

node dist/bin/packages/core/test/render3/perf/${BENCHMARK}_lib.min_debug.js

Profile

node --no-turbo-inlining --inspect-brk dist/bin/packages/core/test/render3/perf/${BENCHMARK}_lib.min_debug.js

then connect with a debugger (the --inspect-brk option will make sure that benchmark execution doesn't start until a debugger is connected and the code execution is manually resumed).

The actual benchmark code has calls that will start (console.profile) and stop (console.profileEnd) a profiling session.

Deoptigate

yarn add deoptigate
yarn deoptigate dist/bin/packages/core/test/render3/perf/${BENCHMARK}_lib.min_debug.js

Run All

To run all of the benchmarks use the profile_all.js script:

node packages/core/test/render3/perf/profile_all.js

NOTE: This command will build all of the tests, so there is no need to do so manually.

Optionally use the --write command to save the run result to a file for later comparison.

node packages/core/test/render3/perf/profile_all.js --write baseline.json

Comparing Runs

If you have saved the baseline (as described in the step above) you can use it to get change in performance like so:

node packages/core/test/render3/perf/profile_all.js --read baseline.json

The resulting output should look something like this:

┌────────────────────────────────────┬─────────┬──────┬───────────┬───────────┬───────┐
│              (index)               │  time   │ unit │ base_time │ base_unit │   %   │
├────────────────────────────────────┼─────────┼──────┼───────────┼───────────┼───────┤
│       directive_instantiate        │ 276.652 │ 'ms' │  286.292  │   'ms'    │ -3.37 │
│        element_text_create         │ 262.868 │ 'ms' │  260.031  │   'ms'    │ 1.09  │
│           interpolation            │ 257.733 │ 'us' │  260.489  │   'us'    │ -1.06 │
│             listeners              │  1.997  │ 'us' │   1.985   │   'us'    │  0.6  │
│ map_based_style_and_class_bindings │  10.07  │ 'ms' │   9.786   │   'ms'    │  2.9  │
│       noop_change_detection        │ 93.256  │ 'us' │  91.745   │   'us'    │ 1.65  │
│          property_binding          │ 290.777 │ 'us' │  280.586  │   'us'    │ 3.63  │
│      property_binding_update       │ 588.545 │ 'us' │  583.334  │   'us'    │ 0.89  │
│      style_and_class_bindings      │  1.061  │ 'ms' │   1.047   │   'ms'    │ 1.34  │
│           style_binding            │ 543.841 │ 'us' │  545.385  │   'us'    │ -0.28 │
└────────────────────────────────────┴─────────┴──────┴───────────┴───────────┴───────┘

Notes

To run the benchmark use bazel run <benchmark_target>, example:

  • yarn bazel run --config=ivy //packages/core/test/render3/perf:noop_change_detection

To profile, append _profile to the target name and attach a debugger via chrome://inspect, example:

  • yarn bazel run --config=ivy //packages/core/test/render3/perf:noop_change_detection_profile

To interactively edit/rerun benchmarks use ibazel instead of bazel.

To debug

  • Follow the directions in profile_in_browser.html OR
  • yarn bazel build --config=ivy //packages/core/test/render3/perf:noop_change_detection
  • node --inspect-brk bazel-out/darwin-fastbuild/bin/packages/core/test/render3/perf/noop_change_detection.min_debug.js