Title: Document Megaplot memory performance best practices · Issue #69 · PAIR-code/megaplot · GitHub
Open Graph Title: Document Megaplot memory performance best practices · Issue #69 · PAIR-code/megaplot
X Title: Document Megaplot memory performance best practices · Issue #69 · PAIR-code/megaplot
Description: Create a document which lists performance best practices such as minimizing memory allocations within callbacks. It's common in JavaScript, especially when using D3, to use object literals to pass values. Often in the browser, the perfor...
Open Graph Description: Create a document which lists performance best practices such as minimizing memory allocations within callbacks. It's common in JavaScript, especially when using D3, to use object literals to pass ...
X Description: Create a document which lists performance best practices such as minimizing memory allocations within callbacks. It's common in JavaScript, especially when using D3, to use object literals to p...
Opengraph URL: https://github.com/PAIR-code/megaplot/issues/69
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Document Megaplot memory performance best practices","articleBody":"Create a document which lists performance best practices such as minimizing memory allocations within callbacks.\r\n\r\n---\r\n\r\nIt's common in JavaScript, especially when using D3, to use object literals to pass values. Often in the browser, the performance implication of creating these short-lived objects is negligible compared to DOM operations such as reflow and paint.\r\n\r\nHowever, in Megaplot, memory operations are often among the more expensive, and so developers should take care to avoid creating short-lived objects, especially in callbacks which may be invoked many times.\r\n\r\nFor example, Megaplot allows colors to be specified by their individual component:\r\n\r\n```ts\r\nconst sprite = scene.createSprite();\r\nsprite.enter(s =\u003e {\r\n s.FillColorR = 255;\r\n s.FillColorG = 0;\r\n s.FillColorB = 255;\r\n s.FillColorOpacity = 1;\r\n});\r\n```\r\n\r\nThis is verbose, and so Megaplot offers destructuring assignment as well as individual component assignment.\r\n\r\nHere's the same example, but using destructuring with an object literal:\r\n\r\n```ts\r\nconst sprite = scene.createSprite();\r\nsprite.enter(s =\u003e {\r\n s.FillColor = [255, 0, 255, 1];\r\n});\r\n```\r\n\r\nSince only a single Sprite is being created, and its `enter()` callback is invoked only once, this code has the effect of instatiating only one copy of the array `[255, 0, 255, 1]`.\r\n\r\nThe problem is when the same approach is taken with selections:\r\n\r\n```ts\r\nconst selection = scene.createSelection();\r\nselection.onInit(s =\u003e {\r\n s.FillColor = [255, 0, 255, 1];\r\n});\r\nselection.bind(data);\r\n```\r\n\r\nWhen the selection is bound to the array of data, the selection's `onInit()` callback is called for each Sprite/datum pair. That means that a short-lived copy of the array `[255, 0, 255, 1]` is created and destroyed many times. These allocation and deallocation operations are significantly costlier than mere memory access at scale, and so it's better to declare constants outside of the callbacks like so:\r\n\r\n```ts\r\nconst INIT_FILL_COLOR = [255, 0, 255, 1];\r\nconst selection = scene.createSelection();\r\nselection.onInit(s =\u003e {\r\n s.FillColor = INIT_FILL_COLOR;\r\n});\r\nselection.bind(data);\r\n```\r\n","author":{"url":"https://github.com/jimbojw","@type":"Person","name":"jimbojw"},"datePublished":"2022-10-05T10:44:13.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/69/megaplot/issues/69"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:72d0479b-55a8-9f78-284c-2f899963a930 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D718:88322:3B7FBE:56E8AC:6A4E0450 |
| html-safe-nonce | 54e7677aa564fea95d4cac0079586630ea5a62b4e36d9ec845e8ab16423f9626 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzE4Ojg4MzIyOjNCN0ZCRTo1NkU4QUM6NkE0RTA0NTAiLCJ2aXNpdG9yX2lkIjoiNDc3NTA2MTQ4NjYxNDQ3OTk1MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 77c3eac5e76bf14e03953cbfdd6964a0a428c97f49fad2bc5c1b51c2e28fc4a2 |
| hovercard-subject-tag | issue:1397594303 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PAIR-code/megaplot/69/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2bfed60ff8556118582f7d6a484f5995ef4cb657844aa165adaed169f937f1d9/PAIR-code/megaplot/issues/69 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2bfed60ff8556118582f7d6a484f5995ef4cb657844aa165adaed169f937f1d9/PAIR-code/megaplot/issues/69 |
| og:image:alt | Create a document which lists performance best practices such as minimizing memory allocations within callbacks. It's common in JavaScript, especially when using D3, to use object literals to pass ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jimbojw |
| hostname | github.com |
| expected-hostname | github.com |
| None | df0492960db29b4938cb72070351d6b1d0c6c0767b27ceb8394bbf4fcc0223c6 |
| turbo-cache-control | no-preview |
| go-import | github.com/PAIR-code/megaplot git https://github.com/PAIR-code/megaplot.git |
| octolytics-dimension-user_id | 29804435 |
| octolytics-dimension-user_login | PAIR-code |
| octolytics-dimension-repository_id | 451893434 |
| octolytics-dimension-repository_nwo | PAIR-code/megaplot |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 451893434 |
| octolytics-dimension-repository_network_root_nwo | PAIR-code/megaplot |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 52bde38e24398476f8eb1e0760c81346c6a00812 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width