{"id":13725318,"url":"https://github.com/rescript-association/bs-list-benchmark","last_synced_at":"2025-05-07T20:31:55.063Z","repository":{"id":114563515,"uuid":"194506870","full_name":"rescript-association/bs-list-benchmark","owner":"rescript-association","description":"Discussion of the internal data representation of immutable lists","archived":false,"fork":false,"pushed_at":"2019-07-01T20:17:36.000Z","size":4,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-04T01:27:40.228Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rescript-association.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-06-30T11:12:19.000Z","updated_at":"2020-11-01T04:07:00.000Z","dependencies_parsed_at":"2023-03-22T07:03:29.780Z","dependency_job_id":null,"html_url":"https://github.com/rescript-association/bs-list-benchmark","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rescript-association%2Fbs-list-benchmark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rescript-association%2Fbs-list-benchmark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rescript-association%2Fbs-list-benchmark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rescript-association%2Fbs-list-benchmark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rescript-association","download_url":"https://codeload.github.com/rescript-association/bs-list-benchmark/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224645327,"owners_count":17346124,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:02:19.352Z","updated_at":"2024-11-14T15:31:11.293Z","avatar_url":"https://github.com/rescript-association.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# BuckleScript List Benchmark\n\nThis benchmark demonstrates how to optimize list iteration speed in almost all major\nJS engines by tweaking the internal runtime representation.\n\nAs for right now, the expression `let myList = [\"a\", \"b\", \"c\"]` is compiled to\nfollowing JavaScript:\n\n```\nvar myList = /* :: */[\n  \"a\",\n  /* :: */[\n    \"b\",\n    /* :: */[\n      \"c\",\n      /* [] */0\n    ]\n  ]\n];\n```\n\nNote how the last element in the array is terminated with a `0` value. This\nwill cause V8 to do a [kind\ntransition](https://v8.dev/blog/elements-kinds#avoid-elements-kind-transitions)\nto a polymorphic kind [which ideally should be\navoided](https://v8.dev/blog/elements-kinds#avoid-polymorphism).\n\nTo prevent this, the internal list representation must be terminated with a non-SMI value, such as\n`false`, `null`, etc:\n\n```\nvar myList = /* :: */[\n  \"a\",\n  /* :: */[\n    \"b\",\n    /* :: */[\n      \"c\",\n      /* [] */null\n    ]\n  ]\n];\n```\n\n## Setup\n\n```\nnpm install jsvu -g\n\n# Make sure to tick chakra, javascriptcore, spidermonkey, v8 and v8-debug\njsvu\n```\n\n## Run full Benchmark\n\n```\n# Will run the performance test for each JS engine\n./benchmark.sh\n```\n\n**Test Results:**\n\nMacbook Pro 13\" 2016, 3,3 GHz Intel Core i7, 16GB ram\n\nThe benchmark does 3 runs per `original` / `optimized` group to make sure that\nthere are no warm-up side-effects. Each function is called `1e7` times to\nensure an obversable time diff.\n\nThe optimized function uses `null` as list terminator, the original function uses\na `0` (current behavior in BuckleScript). The test source code can be found in [`./perf.js`](./perf.js).\n\n```\n./benchmark.sh\n\nRun V8:\noptimized: 1242 ms.\noriginal: 1722 ms.\noptimized: 1263 ms.\noriginal: 1734 ms.\noptimized: 1233 ms.\noriginal: 1760 ms.\n\nRun JavaSciptCore:\noptimized: 430 ms.\noriginal: 835 ms.\noptimized: 881 ms.\noriginal: 936 ms.\noptimized: 859 ms.\noriginal: 938 ms.\n\nRun SpiderMonkey:\noptimized: 542 ms.\noriginal: 492 ms.\noptimized: 567 ms.\noriginal: 482 ms.\noptimized: 546 ms.\noriginal: 518 ms.\n\nRun Chakra:\noptimized: 1112 ms.\noriginal: 3575 ms.\noptimized: 908 ms.\noriginal: 3479 ms.\noptimized: 894 ms.\noriginal: 3513 ms.\n```\n\nThere are some interesting observations:\n\n- ~30% better V8 / Chakra performance\n- -9% worse performance in SpiderMonkey, but the performance there is already 3 times as fast as V8\n- Not sure why, but in JSC, the first call has a huge gap with +51% performance boost, while every other call is only around +6%\n\n## Credits\n\nThanks to @bmeurer for explaining element kinds mechanic in V8, how to\ninterpret the bytecode debugging output and writing the performance tests.\n\n**Links:**\n\n- [V8 Element Kinds | V8 Dev Blog](https://v8.dev/blog/elements-kinds#the-elements-kind-lattice)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frescript-association%2Fbs-list-benchmark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frescript-association%2Fbs-list-benchmark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frescript-association%2Fbs-list-benchmark/lists"}