|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <benchmark/benchmark.h> |
| 9 | +#include <folly/dynamic.h> |
| 10 | +#include <folly/json.h> |
| 11 | +#include <react/components/view/ViewComponentDescriptor.h> |
| 12 | +#include <react/core/EventDispatcher.h> |
| 13 | +#include <react/core/RawProps.h> |
| 14 | +#include <react/utils/ContextContainer.h> |
| 15 | +#include <exception> |
| 16 | +#include <string> |
| 17 | + |
| 18 | +namespace facebook { |
| 19 | +namespace react { |
| 20 | + |
| 21 | +auto contextContainer = std::make_shared<ContextContainer const>(); |
| 22 | +auto eventDispatcher = std::shared_ptr<EventDispatcher>{nullptr}; |
| 23 | +auto viewComponentDescriptor = |
| 24 | + ViewComponentDescriptor(eventDispatcher, contextContainer); |
| 25 | + |
| 26 | +auto emptyPropsDynamic = folly::parseJson("{}"); |
| 27 | +auto propsString = std::string{ |
| 28 | + "{\"flex\": 1, \"padding\": 10, \"position\": \"absolute\", \"display\": \"none\", \"nativeID\": \"some-id\", \"direction\": \"rtl\"}"}; |
| 29 | +auto propsDynamic = folly::parseJson(propsString); |
| 30 | +auto propsStringWithSomeUnsupportedProps = std::string{ |
| 31 | + "{\"someName1\": 1, \"someName2\": 10, \"someName3\": \"absolute\", \"someName4\": \"none\", \"someName5\": \"some-id\", \"someName6\": \"rtl\"}"}; |
| 32 | +auto unsupportedPropsDynamic = |
| 33 | + folly::parseJson(propsStringWithSomeUnsupportedProps); |
| 34 | + |
| 35 | +auto sourceProps = ViewProps{}; |
| 36 | +auto sharedSourceProps = ViewShadowNode::defaultSharedProps(); |
| 37 | + |
| 38 | +static void emptyPropCreation(benchmark::State &state) { |
| 39 | + for (auto _ : state) { |
| 40 | + ViewProps{}; |
| 41 | + } |
| 42 | +} |
| 43 | +BENCHMARK(emptyPropCreation); |
| 44 | + |
| 45 | +static void propParsingEmptyRawProps(benchmark::State &state) { |
| 46 | + for (auto _ : state) { |
| 47 | + viewComponentDescriptor.cloneProps( |
| 48 | + sharedSourceProps, RawProps{emptyPropsDynamic}); |
| 49 | + } |
| 50 | +} |
| 51 | +BENCHMARK(propParsingEmptyRawProps); |
| 52 | + |
| 53 | +static void propParsingRegularRawProps(benchmark::State &state) { |
| 54 | + for (auto _ : state) { |
| 55 | + viewComponentDescriptor.cloneProps( |
| 56 | + sharedSourceProps, RawProps{propsDynamic}); |
| 57 | + } |
| 58 | +} |
| 59 | +BENCHMARK(propParsingRegularRawProps); |
| 60 | + |
| 61 | +static void propParsingUnsupportedRawProps(benchmark::State &state) { |
| 62 | + for (auto _ : state) { |
| 63 | + viewComponentDescriptor.cloneProps( |
| 64 | + sharedSourceProps, RawProps{unsupportedPropsDynamic}); |
| 65 | + } |
| 66 | +} |
| 67 | +BENCHMARK(propParsingUnsupportedRawProps); |
| 68 | + |
| 69 | +static void propParsingRegularRawPropsWithNoSourceProps( |
| 70 | + benchmark::State &state) { |
| 71 | + for (auto _ : state) { |
| 72 | + viewComponentDescriptor.cloneProps(nullptr, RawProps{propsDynamic}); |
| 73 | + } |
| 74 | +} |
| 75 | +BENCHMARK(propParsingRegularRawPropsWithNoSourceProps); |
| 76 | + |
| 77 | +} // namespace react |
| 78 | +} // namespace facebook |
| 79 | + |
| 80 | +BENCHMARK_MAIN(); |
0 commit comments