1

With the help of a fellow Stack Overflow member I got a code for having shortcuts using Keydown.

How to Check if jQuery .keydown() has any other keys pressed or not?

The code uses setTimeout function for every single keydown event. I am using it on a page where users usually type more than 1000 words. (4k-5k characters I assume)

This means Keydown would run on all those 5k keydowns and new timeouts will starts many times.


    var NewKeys = {};
    var NewTimeout;
    jQuery(document).keydown(function (e) {
        if (NewTimeout) {
            clearTimeout(NewTimeout);
        }
        NewKeys[e.which] = true;
        NewTimeout  = setTimeout(function () {
            if (typeof NewKeys[17] !== "undefined"  && typeof NewKeys[83] !== "undefined"  && Object.keys(NewKeys).length === 2) { 
                e.preventDefault();
                if(!jQuery('.SearchCreations').hasClass('Show')){
                    e.preventDefault();
                    jQuery('.SaveCreation').click();
                }
            }
            if (typeof NewKeys[27] !== "undefined" && Object.keys(NewKeys).length === 1) { 
                jQuery('.Thumbnail.Close').click();
                jQuery('.Action').removeClass('Collapse');
            }
            if (typeof NewKeys[16] !== "undefined" && typeof NewKeys[17] !== "undefined"  && typeof NewKeys[83] !== "undefined"  && Object.keys(NewKeys).length === 3) {
                e.preventDefault();
                if(jQuery('.CreationEditor').hasClass('Autosaving')){
                    jQuery('.StopSave').click();
                }
                else{
                    jQuery('.AutoSave').click();
                }
            }
          }, 200);
    });

Can this affect performance/reliability for the end user.

The last thing you want to do is have a webpage that is so resource intensive that your user's device stops working properly.

I tried this code on 6-7 year old laptop (i5, 4GB RAM, Windows 10). I noticed that if I keep using the page it works fine. But if I switch window to some other application using Alt + Tab and return back to browser in about 5-7 sec to my site, the Shortcut keys would stop working.

Can confirm this behavior is consistent, and it is reproducable on multiple Windows 10 machines as of now.

Ion Bazan
  • 723
  • 6
  • 16
Aditya Agarwal
  • 52
  • 1
  • 14
  • 1
    It should be relatively easy to test by running some performance profiles in the devtools with listeners added and listeners omitted. For more context, _why_ are you doing this? Perhaps you have inadvertently fallen into an antipattern where a better approach exists? Is there any reason you can't implement some form of throttling, debouncing, or batching in order to achieve the same effect with less discrete timeouts running? – Alexander Nied Sep 09 '21 at 04:23
  • Right. Well, I suppose the dev tools only check performance on my machines. And at the end the shortcuts stop working on multiple moderately speced machines. (The error is reproducable). And as far as better approach, I couldn't find any, there's not much information about implementing REAL shortcuts in jquery... refer to the other question I linked in my question to understand why this is the only approach I found in internet. – Aditya Agarwal Sep 09 '21 at 04:40

1 Answers1

0

This pretty much depends on your timeout value and also the speed of your typing. For example your timeout is 3 and you type 5 letters in second. Then you will end up with 15 functions that run simultaneously.

It's fine, but when you use a longer timeout, like maybe 10s and your speed is 10 letters/sec, then you will have 100 functions running at the same time which is not appropriate.

Specially if you try to do heavier tasks than changing classes and some DOM manipulations.

JW Geertsma
  • 857
  • 3
  • 13
  • 19
  • Well, There's a wide range of users currently on my site, and obviously everyone has different typing speed. What I definitely know is that the keyboard shortcut stops running in every single laptop that I tested when I move to some other window by using Alt + Tab, this includes time with reallly slow typing rate too. And yes as shown in code the timeout is set to 200 = 0.2s – Aditya Agarwal Sep 09 '21 at 06:16