-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useMemory): new function (#923)
Co-authored-by: Jelf <haichao.liang@parameters.cn> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
4755036
commit 8508d28
Showing
6 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<template> | ||
<div v-if="isSupported && memory" class="inline-grid grid-cols-2 gap-x-4 gap-y-2"> | ||
<template v-if="memory"> | ||
<div opacity="50"> | ||
Used | ||
</div><div>{{ size(memory.usedJSHeapSize) }}</div> | ||
<div opacity="50"> | ||
Allocated | ||
</div><div>{{ size(memory.totalJSHeapSize) }}</div> | ||
<div opacity="50"> | ||
Limit | ||
</div><div>{{ size(memory.jsHeapSizeLimit) }}</div> | ||
</template> | ||
</div> | ||
<div v-else> | ||
Your browser does not support performance memory API | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { useMemory } from '@vueuse/core' | ||
const size = (v: number) => { | ||
const kb = v / 1024 / 1024 | ||
return `${kb.toFixed(2)} MB` | ||
} | ||
const { isSupported, memory } = useMemory() | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
category: Browser | ||
--- | ||
|
||
# useMemory | ||
|
||
Reactive Memory Info. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useMemory } from '@vueuse/core' | ||
|
||
const {isSupported, memory} = useMemory() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { ref } from 'vue-demi' | ||
import { IntervalFnOptions, useIntervalFn } from '@vueuse/core' | ||
|
||
/** | ||
* Performance.memory | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory | ||
*/ | ||
export interface MemoryInfo { | ||
/** | ||
* The maximum size of the heap, in bytes, that is available to the context. | ||
*/ | ||
readonly jsHeapSizeLimit: number | ||
/** | ||
* The total allocated heap size, in bytes. | ||
*/ | ||
readonly totalJSHeapSize: number | ||
/** | ||
* The currently active segment of JS heap, in bytes. | ||
*/ | ||
readonly usedJSHeapSize: number | ||
|
||
[Symbol.toStringTag]: 'MemoryInfo' | ||
} | ||
|
||
export interface MemoryOptions extends IntervalFnOptions { | ||
interval?: number | ||
} | ||
|
||
type PerformanceMemory = Performance & { | ||
memory: MemoryInfo | ||
} | ||
|
||
/** | ||
* Reactive Memory Info. | ||
* | ||
* @see https://vueuse.org/useMemory | ||
* @param options | ||
*/ | ||
export function useMemory(options: MemoryOptions = {}) { | ||
const memory = ref<MemoryInfo>() | ||
const isSupported = performance && 'memory' in performance | ||
|
||
if (isSupported) { | ||
const { interval = 1000 } = options | ||
useIntervalFn(() => { | ||
memory.value = (performance as PerformanceMemory).memory | ||
}, interval, { immediate: options.immediate, immediateCallback: options.immediateCallback }) | ||
} | ||
|
||
return { isSupported, memory } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters