Skip to content

Commit

Permalink
optimize fillStyle to be 10% faster
Browse files Browse the repository at this point in the history
this is possibly a hot path, and avoiding the C++ string helps a
little bit
  • Loading branch information
chearon committed Jun 24, 2023
1 parent 42ea2ff commit 83d8af0
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2097,8 +2097,11 @@ Context2d::_setFillColor(Napi::Value arg) {
short ok;

if (stringValue.IsJust()) {
std::string str = stringValue.Unwrap().Utf8Value();
uint32_t rgba = rgba_from_string(str.c_str(), &ok);
Napi::String str = stringValue.Unwrap();
char buf[128] = {0};
napi_status status = napi_get_value_string_utf8(env, str, buf, sizeof(buf) - 1, nullptr);
if (status != napi_ok) return;
uint32_t rgba = rgba_from_string(buf, &ok);
if (!ok) return;
state->fillPattern = state->fillGradient = NULL;
state->fill = rgba_create(rgba);
Expand Down

0 comments on commit 83d8af0

Please sign in to comment.