{"id":16014390,"url":"https://github.com/vitkarpov/stljs","last_synced_at":"2025-09-09T22:14:07.288Z","repository":{"id":44143258,"uuid":"186225723","full_name":"vitkarpov/stljs","owner":"vitkarpov","description":"It's STL (👋 C++ fans) for your {Type,Java}Script projects.","archived":false,"fork":false,"pushed_at":"2023-01-03T21:45:32.000Z","size":674,"stargazers_count":4,"open_issues_count":15,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-28T03:41:09.949Z","etag":null,"topics":["deque","heaps","javascript","stl","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/vitkarpov.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}},"created_at":"2019-05-12T07:39:14.000Z","updated_at":"2022-07-25T01:44:36.000Z","dependencies_parsed_at":"2023-02-01T10:32:06.053Z","dependency_job_id":null,"html_url":"https://github.com/vitkarpov/stljs","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/vitkarpov%2Fstljs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitkarpov%2Fstljs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitkarpov%2Fstljs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitkarpov%2Fstljs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vitkarpov","download_url":"https://codeload.github.com/vitkarpov/stljs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243878483,"owners_count":20362433,"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":["deque","heaps","javascript","stl","typescript"],"created_at":"2024-10-08T15:02:18.877Z","updated_at":"2025-03-17T20:30:29.555Z","avatar_url":"https://github.com/vitkarpov.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# STL.js\n\n![](./assets/stl.png)\n\n**Standard Template Library for {Type,Java}Script projects**\n\n## Why?\n\nHave you ever been in a situation where you need to use a deque but you don't have such a thing in JavaScript?\n\"Why don't you use an array?\", they might ask. I'd like to give an answer: **time complexity**\n\nLet's take a look at the deque example. It has `O(1)` for `{push,pop}_{front,back}` (C++ API) operations, in a word, it's a doubly linked list and you can push and pop items to boths ends fast. You can't take an arbitrary element by index as you can do with arrays, and that's the price you pay. That's fine since you don't need to, doubly linked lists serve the different purpose.\n\nWhat's about `unshift` complexity of `Array` in V8? Benchmarks show it's significantly slower than `push`. It moves the elements to relayout memory, as you might expect, it's `O(n)` in worst case.\n\n## Deque\n\nCompliant with **JavaScript Array API**.\n\n### Benchmarks 🚀\n\n`Array#unshift` works in `O(n)` time and `Deque#unshift` works in `O(1)` time, check out the benchmarks:\n\n| Name          | Total iterations | Iterations per second  |\n| ------------- | ---------------- | ---------------------- |\n| deque#unshift | 46,938           | 10,672                 |\n| array#unshift |    325           |     74                 |\n\n`Array#shift` is optimized though, works **faster** than `Deque#shift` on ~20%:\n\n| Name          | Total iterations | Iterations per second  |\n| ------------- | ---------------- | ---------------------- |\n| array#shift   | 2,785,723,217    | 633,983,435            |\n| deque#shift   | 2,263,189,082    | 515,650,280            |\n\n`Array#push` and `Array#pop` works faster as expected:\n\n| Name          | Total iterations | Iterations per second  |\n| ------------- | ---------------- | ---------------------- |\n| array#push    | 55,567           | 13,214                 |\n| deque#push    | 23,356           |  5,278                 |\n\n| Name          | Total iterations | Iterations per second  |\n| ------------- | ---------------- | ---------------------- |\n| array#pop     | 1,712,950,723    | 1,334,438,329          |\n| deque#pop     |   388,688,614    |   301,159,631          |\n\n\u003e Note: tests run ~4s on 10.000 items\n\n### API\n\n#### push\n\n```ts\nimport { Deque } from 'stljs';\nconst deque = new Deque\u003cnumber\u003e()\ndeque.push(1);\n// 1\ndeque.back();\n```\n\n#### pop\n\n```ts\nimport { Deque } from 'stljs';\nconst deque = new Deque\u003cnumber\u003e()\ndeque.push(1);\n// 1\ndeque.pop();\n```\n\n#### unshift\n\n```ts\nimport { Deque } from 'stljs';\nconst deque = new Deque\u003cnumber\u003e()\ndeque.push(2);\ndeque.push(3);\ndeque.unshift(1);\n// 1\ndeque.front();\n```\n\n#### shift\n\n```ts\nimport { Deque } from 'stljs';\nconst deque = new Deque\u003cnumber\u003e()\ndeque.push(1);\ndeque.push(2);\ndeque.push(3);\n// 1\ndeque.shift();\n```\n\n#### length\n\n```ts\nimport { Deque } from 'stljs';\nconst deque = new Deque\u003cnumber\u003e()\ndeque.push(1);\ndeque.push(2);\ndeque.push(3);\n\nwhile (deque.length \u003e 0) {\n  // 3, 2, 1\n  deque.pop();\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitkarpov%2Fstljs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvitkarpov%2Fstljs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitkarpov%2Fstljs/lists"}