Tuesday, August 8, 2017

Javascript Memory Limit and Usage in Chrome



<script>
// Thank you stackoverflow user B. Gruenbaum
console.log('performance.memory.jsHeapSizeLimit='+performance.memory.jsHeapSizeLimit);
// will give you the JS heap size
console.log('performance.memory.usedJSHeapSize='+performance.memory.usedJSHeapSize);
// how much you're currently using
</script>

Slightly off topic from post title, the following may be an issue in the problem set being thought on in regards to memory limits: JSON object sizes.
In my scenario I had static reference data in CSV format. I converted to JSON files ( e.g. materials.json).  Here is standalone PHP script you can place in directory of JSON files and get file sizes and javascript memory info all together.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<meta name="format-detection" ></meta>
<title>JS Memory Size</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<?php
foreach(glob('*.json') as $file) {
   print "<script type='text/javascript' src='".$file."' ></script>".PHP_EOL;
}
?>

<script>
$(document).ready(function(){
  console.log('performance.memory.jsHeapSizeLimit='+performance.memory.jsHeapSizeLimit);
  // will give you the JS heap size
  console.log('performance.memory.usedJSHeapSize='+performance.memory.usedJSHeapSize);
  // how much you're currently using
  $('#jsHeapSizeLimit').text(performance.memory.jsHeapSizeLimit);
  $('#usedJSHeapSize').text(performance.memory.usedJSHeapSize);
});
</script>
<style>
td {
align:left;
border:1px dotted #000000;
padding:5px;
}
</style>
</head>
<body>
<h1>JS Memory Size</h1>
<table><tr>
<th>Size</th><th>Info</th>
</tr>
 <td id='jsHeapSizeLimit'></td><td>Size Limit</td>
</tr><tr>
 <td id='usedJSHeapSize'></td><td>Currently Used</td>
</tr>
<?php
foreach(glob('*.json') as $file) {
   print "<tr><td>".filesize($file)."</td><td>".$file."</td></tr>".PHP_EOL;
}
?>
</table>
</body>
</html>
<?php
die();
?>

Thursday, June 8, 2017

Dynamically create input name and id via associated label text

var groupA = "";
$('label').each(function(index, item) {
var a = $(this).text();
a = a.replace(/\W/g, '');
a = a.replace(/\s/g, '');
a = a.toLowerCase();
groupA = $(this).attr('group');
$('input:text').each(function(index,item) {
if ($(this).attr('group') == groupA) {
$(this).attr('name', a + $(this).attr('typeB') );
$(this).attr('id', a + $(this).attr('typeB') );
}
});
});

Tuesday, April 4, 2017

Force use of HTTPS with Javascript

<script>
location.protocol=='https:' ? console.log(window.location.href) : replace('https:'+location.hostname+location.pathname);
</script>

Friday, March 17, 2017

See inventory of function in javascript files

Place this script in same directory your javascript files are, view in browser.
(and yes I know there is grep -n 'function' *.js but I like things I can see in browser)
 <?php
header("Content-Type:text/plain") ;
$files=glob('*.js');
$searchfor = 'function';
foreach($files as $file) {
print $file.PHP_EOL;
$contents=file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
  print count($matches[0])." total functions".PHP_EOL;
  print implode("\n", $matches[0]);
}
}
?>

Example out:

main.js
37 total functions
function checkExists(myVar) {
function consoleLogTime(input) {
function debugMsg(input) {
function getQuerystring(key, default_)
function selectCustomer(input) {
function listCreate(arrayValues, arrayText, myClass) {
function optionListCreate(arrayValues, arrayText) {
function validateEmail(x) {
function checkEmailExists(email) {
      success: function(result) {
      error: function(result) {
function passwordReset(email) {
      success: function(result) {
      error: function(result) {
function listArray(myArray) { 
function getJsonTable(url, table, tag) {
 $.getJSON(url+'?table='+table, function(data){
        $.each(data, function(index,item) {
$(document).ready(function(){
     success: function(result) {
     error: function(result) {
$('#signin').click(function() {
$('#signup').click(function() {
$("#passwordReset").click(function() { 
$("#emailB").blur(function() { 
$('.mytype').click(function() {
$("#passwordconfirmB").keyup(function() { 
$("#passwordconfirmB").blur(function() { 
$("#workorder").ready(function() { 
$("select").ready(function() { 
$("#bids").change(function() { 
$("#about").click(function() { 
$('.submit').click(function() {
      success: function(result) {
      error: function(result) {
$("#selectacustomer").click(function() { 
$(".customerSelect").click(function() { 


Thursday, February 2, 2017

Wednesday, February 1, 2017

Web Browser Sniffing

function get_browser_name($user_agent)
{
    if (strpos($user_agent, 'opera') || strpos($user_agent, 'opr/')) return 'opera';
    elseif (strpos($user_agent, 'edge')) return 'edge';
    elseif (strpos($user_agent, 'chrome')) return 'chrome';
    elseif (strpos($user_agent, 'safari')) return 'safari';
    elseif (strpos($user_agent, 'firefox')) return 'firefox';
    elseif (strpos($user_agent, 'msie') || strpos($user_agent, 'trident/7')) return 'internet explorer';
    elseif (strpos($user_agent, 'ipad')) return 'ipad';
    
    return 'Other';
}

$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
$browser = get_browser_name($userAgent);

(javascript)
var userAgent = '';
var browser = '';
console.log('userAgent '+userAgent);
console.log('browser '+browser);

Tuesday, January 17, 2017

Javascript checkExists

function checkExists(myVar) {

 if (typeof myVar === 'undefined')  { return false;  }
 if ( myVar === null ) { return false;  }
 if ( myVar.length == 0) { return false; } 
 
 return true;
 
}