Addsy
Addsy

Reputation: 4054

Is there a way to throttle javascript performance to simulate a slow client

I am working on a site that uses jQuery and has a fair amount of javascript that is run using $(document).ready(). On my dev machine everything runs great but it's a pretty powerful machine. I have had reports from people using older hardware of behavior that seems strange and I am fairly convinced that it is down to the time taken to process this initial javascript on slower machines.

Clearly, the solution is to sort out this initial javascript but it got me wondering - does anyone know of a way to slow down the execution speed of javascript in either Chrome or Firefox to be able to simulate these slower clients on my dev machine?

Cheers!

Update: Back when this question was posted, there weren't the same set of tools that there are today. At that time the VM option was the best option therefore I am leaving it as the accepted answer. However these days I would go straight for Chrome dev tools instead as suggested by Oded Niv

Upvotes: 77

Views: 34382

Answers (11)

Ahmed Hassan
Ahmed Hassan

Reputation: 49

You can fake slow performance with javascript like below

const btn = document.getElementById('btn')
const btn2 = document.getElementById('btn2')
const para = document.getElementById('para')

      
btn.addEventListener('click', ()=> {
  let startTime = performance.now();
  while (performance.now() - startTime < 700) {
    // Do nothing for 700 ms to emulate extremely slow code
  }

  para.innerHTML = 'CONTENT'
})

btn2.addEventListener('click', ()=> {
  para.innerHTML = ''
})
<button id="btn">PRINT</button>
<p id="para"></p>
<button id="btn2">Clear to try again</button>

Upvotes: -2

Oded Niv
Oded Niv

Reputation: 2737

Under Chrome developer tools -> Timeline you now an option to throttle down the CPU, look for the dropdown:

Chrome CPU throttling

UPDATE:

Chrome(ium) changed in new versions, it is now under the Performance tab, and you have to click the settings button in the corner for this feature to show up:

Chrome new CPU throttling

Upvotes: 140

Vladislav Kostenko
Vladislav Kostenko

Reputation: 1205

You can also try to throttle down your CPU via Power management settings in your OS. For example for Win 8 you can go to something like "Control Panel\System and security\Power management\Change you power scheme->Change advanced power settings->CPU power management->Max CPU frequency level" (sorry, this is translation from non English Win8 UI, but I think it is not hard to find the settings). This helps in some degree.

Upvotes: 0

GSK
GSK

Reputation: 5

Use Fiddler - its free - allows you to simulate the connection speed. dialup, ADSL etc.

http://www.telerik.com/fiddler

Upvotes: -4

techfoobar
techfoobar

Reputation: 66693

Easier than a virtual machine i believe will be some kind of a tool that can slow down selected apps (in your case the browser process).

Well you can always try setting the priority of your browser process to the lowest value.

Additionally you can try one of these tools. They are basically meant for slowing down the system/specific-apps so old games can be played on new systems. Will probably fit your test case.

http://moslo.info/

http://www.reocities.com/kulhain/

http://www.sierrahelp.com/Utilities/SlowdownUtilities.html

Upvotes: 1

isJustMe
isJustMe

Reputation: 5470

This might not be the best solution, but something that could definetely work is to run a virtual machine, there you could specify all hardware specs as long as they are lower than you real machine. Look at this post

Upvotes: 15

Nikoloz Shvelidze
Nikoloz Shvelidze

Reputation: 1614

Virtualisation is the answer! You may use VirtualBox, it's free. You can simulate a slower machine with it

Upvotes: 1

gavaletz
gavaletz

Reputation: 338

I would use a VM and just limit it's resources. If you are not a fan of virtual machines, then I would go find an old machine at a yard sale, thrift store etc. and use that as a testing platform. You can never patch it, fill it up with crappy malware laden programs and then it will be just like the experience for an "average user." :-)

Upvotes: 12

Jage
Jage

Reputation: 8096

I made this and within like a minute my firefox was sucking up all my memory. You could prolly slow the overload by changing the setTimeout() to something higher. Pretty much made everything run slow, switching tabs and other page loads too.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
    body, html{
        height:100%; 
    }

</style>
<script language="javascript" type="text/javascript">

var NUM = 0;
function addMore(){
    var cur = document.getElementById('ta').value;
    var newVal = NUM.toString();
    if(cur){
        newVal = newVal+cur;
    }
    NUM++;
    document.getElementById('ta').value = newVal;
    setTimeout("addMore()",1);
}

</script>
</head>

<body onload="addMore()">

<textarea id="ta" style="width:80%; height:80%;">0</textarea>

</body>
</html>

Upvotes: -2

Mark Lutton
Mark Lutton

Reputation: 7027

I don't know of anything you can depend on or control, but you might try installing two antivirus programs (or at least as many as the number of CPU cores you have), starting a full scan on each and testing with those full scans running.

Upvotes: -4

SLaks
SLaks

Reputation: 887767

Run Folding@Home in the background to eat up the CPU.

If you have a multi-core processor, use Task Manager to limit IE to a single core, and perhaps also limit some CPU-intensive applications to the same core.

Upvotes: 0

Related Questions