{"id":28629821,"url":"https://github.com/codebox/js-heap","last_synced_at":"2026-06-20T20:31:40.757Z","repository":{"id":140514685,"uuid":"275200914","full_name":"codebox/js-heap","owner":"codebox","description":null,"archived":false,"fork":false,"pushed_at":"2020-06-27T14:15:20.000Z","size":66,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-04T12:40:07.838Z","etag":null,"topics":["algorithms","data-structures","javascript"],"latest_commit_sha":null,"homepage":"https://codebox.net/pages/javascript-binary-heap","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/codebox.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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-06-26T16:31:04.000Z","updated_at":"2020-06-27T14:16:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"e6b6992b-7f6d-4800-a113-7cf8d356b70c","html_url":"https://github.com/codebox/js-heap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/codebox/js-heap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codebox%2Fjs-heap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codebox%2Fjs-heap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codebox%2Fjs-heap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codebox%2Fjs-heap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codebox","download_url":"https://codeload.github.com/codebox/js-heap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codebox%2Fjs-heap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34585195,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["algorithms","data-structures","javascript"],"created_at":"2025-06-12T12:13:40.297Z","updated_at":"2026-06-20T20:31:40.752Z","avatar_url":"https://github.com/codebox.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JavaScript Binary Heap\n\nHere's a simple JavaScript implementation of a Binary Heap,\n a data structure that can be used as a [Priority Queue](https://en.wikipedia.org/wiki/Priority_queue) storing items and returning\nthem in priority order. The code uses a number of ECMAScript 6 features and therefore will not work on older browsers.\n\nI've implemented `insert`, `extract` and `peek` methods because that was all I needed at the time, but\nit should be simple to extend if required:\n\nMethod|Time Complexity (Worst Case)\n------|-----------\ninsert()|O(log n)\nextract()|O(log n)\npeek()|O(1)\n\nTo create a heap object, just call the `buildHeap()` function and add new items using the `insert()` method.\nBy default a 'min-heap' is created, where the smallest remaining item will be returned next:\n\n\n    const heap = buildHeap();\n    \n    heap.insert(3);\n    heap.insert(2);\n    heap.insert(1);\n    \n    heap.extract(); // 1\n    heap.extract(); // 2\n    heap.extract(); // 3\n    heap.extract(); // undefined - heap is now empty\n\n`buildHeap()` accepts an optional argument which will be used as a mapping function, transforming values before comparing them\nwith each other. In this way custom ordering can be implemented, for example to create a heap which stores objects and always returns the\none with the highest `score` property, do this:\n\n    const maxScoreHeap = buildHeap(v =\u003e -v.score);\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodebox%2Fjs-heap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodebox%2Fjs-heap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodebox%2Fjs-heap/lists"}