Categories
JavaScript Answers

How to programmatically get memory usage in Chrome with JavaScript?

Sometimes, we want to programmatically get memory usage in Chrome with JavaScript.

In this article, we’ll look at how to programmatically get memory usage in Chrome with JavaScript.

How to programmatically get memory usage in Chrome with JavaScript?

To programmatically get memory usage in Chrome with JavaScript, we can use the window.performance.memory property.

For instance, we write:

console.log(window.performance.memory)

to log the current memory usage in Chrome.

As a result, we get something like:

{
  "jsHeapSizeLimit": 2172649472,
  "totalJSHeapSize": 6722851,
  "usedJSHeapSize": 5630251
}

logged.

All the numbers are in bytes.

jsHeapSizeLimit is the maximum size of the heap that is available to the context.

totalJsHeapSize is current size of the JavaScript heap including free space not occupied by any JavaScript objects.

And usedJSHeapSize is the currently active segment of JS heap.

Conclusion

To programmatically get memory usage in Chrome with JavaScript, we can use the window.performance.memory property.

Categories
JavaScript Answers

How to check if an input field has focus with JavaScript?

Sometimes, we want to check if an input field has focus with JavaScript.

In this article, we’ll look at how to check if an input field has focus with JavaScript.

How to check if an input field has focus with JavaScript?

To check if an input field has focus with JavaScript, we can use the document.activeElement property to get the element in focus.

For instance, we write:

<input>

to add an input.

Then we write:

console.log(document.querySelector('input') === document.activeElement)

to check if the input element is focused.

document.activeElement‘s value is the element that’s current in focus.

Conclusion

To check if an input field has focus with JavaScript, we can use the document.activeElement property to get the element in focus.

Categories
JavaScript Answers

How to convert Blob to JSON with the JavaScript File API?

Sometimes, we want to convert Blob to JSON with the JavaScript File API.

In this article, we’ll look at how to convert Blob to JSON with the JavaScript File API.

How to convert Blob to JSON with the JavaScript File API?

To convert Blob to JSON with the JavaScript File API, we can use the FileReader constructor.

For instance, we write:

const b = new Blob([JSON.stringify({
  "test": "toast"
})], {
  type: "application/json"
})
const fr = new FileReader();

fr.onload = (e) => {
  console.log(JSON.parse(e.target.result))
};

fr.readAsText(b);

We create a blob with a JSON string as its content with the Blob constructor.

We set the type to 'application/json' to set the blob type to JSON.

Next, we create a FileReader instance and set the onload property to a function that gets the parsed result from e.target.result.

Then we call JSON.parse to convert the parsed JSON blob string to an object.

Finally, we call readAsText with blob b to read the blob into a string.

Therefore, the console log should log {test: 'toast'}.

Conclusion

To convert Blob to JSON with the JavaScript File API, we can use the FileReader constructor.

Categories
JavaScript Answers

How to check if text is in a string with JavaScript?

Sometimes, we want to check if text is in a string with JavaScript.

In this article, we’ll look at how to check if text is in a string with JavaScript.

How to check if text is in a string with JavaScript?

To check if text is in a string with JavaScript, we can use the JavaScript string’s includes method.

For instance, we write:

const str = "car, bicycle, bus";
const str2 = "car";
console.log(str.includes(str2));

We call str.includes with str2 to check whether the str2 string’s content is in str1.

Therefore, the console log should log true since 'car' is in 'car, bicycle, bus'.

Conclusion

To check if text is in a string with JavaScript, we can use the JavaScript string’s includes method.

Categories
JavaScript Answers

How to localize a simple HTML web page?

Sometimes, we want to localize a simple HTML web page.

In this article, we’ll look at how to localize a simple HTML web page.

How to localize a simple HTML web page?

To localize a simple HTML web page, we can set the lang attribute of the text elements and then use CSS to show or hide them according to the browser language.

For instance, we write:

<span lang="en">Scale</span>
<span lang="de">Maßstab</span>

to add the spans with text for different languages.

We distinguish them with the lang attribute.

Then we write:

[lang] {
  display: none;
}

[lang=en] {
  display: unset;
}

to select the items with different lang values.

And we set the display CSS property accordingly.

Conclusion

To localize a simple HTML web page, we can set the lang attribute of the text elements and then use CSS to show or hide them according to the browser language.