{"id":15386742,"url":"https://github.com/chrvadala/sliding-window-max","last_synced_at":"2025-03-27T22:48:42.077Z","repository":{"id":57363178,"uuid":"156979530","full_name":"chrvadala/sliding-window-max","owner":"chrvadala","description":"Given a stream of data, this algorithm returns (for every added value) the current max value.","archived":false,"fork":false,"pushed_at":"2020-06-01T11:21:04.000Z","size":745,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T13:56:28.360Z","etag":null,"topics":["algorithm","javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrvadala.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"custom":"https://www.paypal.me/chrvadala/25"}},"created_at":"2018-11-10T12:34:33.000Z","updated_at":"2020-06-01T11:16:57.000Z","dependencies_parsed_at":"2022-09-26T16:32:19.924Z","dependency_job_id":null,"html_url":"https://github.com/chrvadala/sliding-window-max","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrvadala%2Fsliding-window-max","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrvadala%2Fsliding-window-max/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrvadala%2Fsliding-window-max/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrvadala%2Fsliding-window-max/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrvadala","download_url":"https://codeload.github.com/chrvadala/sliding-window-max/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245937977,"owners_count":20696989,"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":["algorithm","javascript"],"created_at":"2024-10-01T14:50:19.244Z","updated_at":"2025-03-27T22:48:42.051Z","avatar_url":"https://github.com/chrvadala.png","language":"JavaScript","funding_links":["https://www.paypal.me/chrvadala/25"],"categories":[],"sub_categories":[],"readme":"# sliding-window-max\n\n[![chrvadala](https://img.shields.io/badge/website-chrvadala-orange.svg)](https://chrvadala.github.io)\n[![Test](https://github.com/chrvadala/sliding-window-max/workflows/Test/badge.svg)](https://github.com/chrvadala/sliding-window-max/actions)\n[![Coverage Status](https://coveralls.io/repos/github/chrvadala/sliding-window-max/badge.svg?branch=master)](https://coveralls.io/github/chrvadala/sliding-window-max?branch=master)\n[![npm](https://img.shields.io/npm/v/sliding-window-max.svg?maxAge=2592000?style=plastic)](https://www.npmjs.com/package/sliding-window-max)\n[![Downloads](https://img.shields.io/npm/dm/sliding-window-max.svg)](https://www.npmjs.com/package/sliding-window-max)\n[![Donate](https://img.shields.io/badge/donate-PayPal-green.svg)](https://www.paypal.me/chrvadala/25)\n\nGiven a stream of data, this algorithm returns (for every added value) the current max value.\n\nIt uses a strategy that:\n - Stores at most a number of values defined by the window size\n - Performs a rolling max calculation\n\n## Example\n\n```javascript\nconst SlidingWindowMax = require('sliding-window-max')\n\nconst windowSize = 3\n\nconst slidingWindowMax = new SlidingWindowMax(windowSize)\nslidingWindowMax.add(0) // returns null\nslidingWindowMax.add(5) // returns null\nslidingWindowMax.add(10)// returns 10\nslidingWindowMax.add(7) // returns 10\nslidingWindowMax.add(8) // returns 10\nslidingWindowMax.add(5) // returns 8\nslidingWindowMax.add(3) // returns 8\nslidingWindowMax.add(4) // returns 5\n//etc ...\n```\n\n| VALUE                    | Max   |\n|--------------------------|-------|\n| [0] 5  10  7  8  5  3  4 | `null`|\n| [0  5] 10  7  8  5  3  4 | `null`|\n| [0  5  10] 7  8  5  3  4 | 10    |\n|  0 [5  10  7] 8  5  3  4 | 10    |\n|  0  5 [10  7  8] 5  3  4 | 10    |\n|  0  5  10 [7  8  5] 3  4 | 8     |\n|  0  5  10  7 [8  5  3] 4 | 8     |\n|  0  5  10  7  8 [5  3  4]| 5     |\n|...                       | ...   |\n\n## Reference\n\n### `new SlidingWindowMax(windowSize, options)`\n|Param                |Default            |Description|\n|---------------------|-------------------|-----------|\n|windowSize           |**required**       | Defines how many values should be considered to calculate the max |\n|options.comparator   | `(a, b) =\u003e b - a` | Override the custom comparator function\n|options.waitFullRange| true             | If false the functions returns the max value also if the window size hasn't been reached yet\n\n### `add(value)`\nEvaluate a new value and returns the calculated max value\n\n## Credits\nI made this algorithm starting from this code https://github.com/chihungyu1116/leetcode-javascript/blob/master/239%20Sliding%20Window%20Maximum.js.\nMy requirements were a bit different. The leetcode algorithm requires that all the data are known before it starts.\nWith **sliding-window-max** you can:\n - evaluate the data as soon as they are produced\n - evaluate big array using less memory\n\n## Changelog\n- **0.x** - Beta version\n- **1.0** - First official version\n- **1.1** - Migrates to gh-workflows; Upgrades deps\n\n ## Contributors\n - [chrvadala](https://github.com/chrvadala) (author)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrvadala%2Fsliding-window-max","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrvadala%2Fsliding-window-max","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrvadala%2Fsliding-window-max/lists"}