Skip to content

Commit

Permalink
perf: refactored structureOutput with single reduce (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
Connormiha authored Oct 26, 2023
1 parent e054f6c commit c0f2297
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- remove unnecessary loop from `osMemoryHeapLinux`

### Added

## [15.0.0] - 2023-10-09
Expand Down
29 changes: 14 additions & 15 deletions lib/metrics/osMemoryHeapLinux.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@ const PROCESS_VIRTUAL_MEMORY = 'process_virtual_memory_bytes';
const PROCESS_HEAP = 'process_heap_bytes';

function structureOutput(input) {
const returnValue = {};
return input.split('\n').reduce((acc, string) => {
if (!values.some(value => string.startsWith(value))) {
return acc;
}

input
.split('\n')
.filter(s => values.some(value => s.indexOf(value) === 0))
.forEach(string => {
const split = string.split(':');
const split = string.split(':');

// Get the value
let value = split[1].trim();
// Remove trailing ` kb`
value = value.substr(0, value.length - 3);
// Make it into a number in bytes bytes
value = Number(value) * 1024;
// Get the value
let value = split[1].trim();
// Remove trailing ` kb`
value = value.substr(0, value.length - 3);
// Make it into a number in bytes bytes
value = Number(value) * 1024;

returnValue[split[0]] = value;
});
acc[split[0]] = value;

return returnValue;
return acc;
}, {});
}

module.exports = (registry, config = {}) => {
Expand Down

0 comments on commit c0f2297

Please sign in to comment.