Help / Code minification (Optimisation) (Gmod Base)

Code minification (Optimisation)

Lang 🇬🇧 Lang 🇫🇷

imgNorda Scripts Creator of this doc
Document Updated ago
By default, my scripts are not indented when downloaded from the site. However, I plan to add an option for users to download the original scripts with indentation, to give them the choice of opting for easier-to-read code. Nevertheless, the code will still be minified by default.

Minification is a process aimed at reducing the size of a file by eliminating elements that are not essential for it to function properly. This increases performance and optimises referencing, particularly in the web context. Although the beauty of the code may take up space, it is important to consider that its main role is to provide a functional service.

The decision not to minify the code can be justified when several people are working on the project, as this makes it easier to read and promotes better organisation within the team.

It is essential to note that the process of conversion to minification only occurs at the time of download. This approach has never affected the maintainability of the source code, despite the numerous updates published every week.

For example, the weight of my finished gamemode is more optimised than that of a simple lambda addon thanks to the complete minification of the source code. Despite its completeness, it also contains an integrated administration system and 540 different objects, all of which are useful, without the need to add any extra addons. This translates into fast load times, even with 300 overloaded NPCs on the map, thousands of drivable vehicles and objects present (although I generally recommend limiting these for optimisation reasons).

In the web context, unminified code can be perceived as unfinished or unoptimised. It is therefore preferable to carry out appropriate minification to obtain efficient, high-performance code.

  1. Introduction
  2. Example of minified code
  3. Frequently asked questions
  4. How to reduce the loading time of users when they join my Gmod server?

1. Introduction

Gmod servers often suffer from loading when there are a lot of scripts and a lot of addons, users take a long time to reach a server and sometimes get discouraged before the end of the loading. Reducing this time could avoid that they leave before the end of the loading, this method applied to all your scripts is not to be neglected.

In programming, minification means to reduce the size of the code to reduce the size of a program to download from a server and thus reduce the network congestion, on Gmod the size of a Lua file on the client side if it is minified will be faster to download. This is one of the many existing optimization rules.

For this we remove all unnecessary comments and characters. These deletions will not interfere with the proper functioning of the application. The download module will also replace the names of the internal variables local to the application to reduce them to one or two characters in an automated way. It is also possible to use some compact writings specific to the languages (color in hexadecimal, shortcuts...)
This process is very much used in web programming and especially very recommended.

2. Example of minified code

Exemple 1: Unminified code
Unminified code
local function my_supermegahyper_test ( Player , Argument , NUMBER )

local firstnumber = NUMBER[1] ;
return firstnumber * 2;
concommand.Add( "give_me_double" , my_supermegahyper_test )

Example 2: Minified code
Minified code
local function test(a,b,c)return c[1]*2 end


3. Frequently asked questions

Question: What is minification?
Answer: Minification is an optimization of the file size by removing unnecessary characters without impacting the proper functioning of the script. It also removes unnecessary spaces and indentation.

Question: What is indentation?
Answer: Indentation consists of adding one or more tabs at the beginning of certain lines in a program to make it more readable.

Question: Does removing the indentation change anything?
Answer: Well, no.

Question: Is removing the indentation a maintenance issue?
Answer: No, those who have experienced my support always appreciate my responsiveness. My source code on my side to work is not the minified version. And at worst when there is an error on line 2 of the init.lua file. Well, you have to expect that there is no need to search more.

Question: Where does minification come from?
Answer: Minification is a form of web optimization recommended by Google to reduce the loading time of sites to the maximum and to the bare minimum necessary to function.

Question: You talk about downloading less heavy files but there are variables that exist in your addons that seem not to be used in the code, why?
Answer: This could be the case with the scripts from 2015 to 2017 when I was on Gmodstore. A lot spread by the leak of my scripts. Today if a variable seems useless it's because you are making a mistake to consider that they can't work in combination with other of my products that will need its variables to work well.

Question: Why did you make this choice?
Answer: I made this choice by default to reduce the size of the files. Smaller files take less space in the cache usually stored in memory, and downloaded by the client at its connection, so a choice that impacts the loading time for Lua files because less large Lua files to download for your users, choice that I also apply here on this site and that pays off in performance. We all know that a user will leave a page more easily if its loading time is too long. Test this site on Google speed for example in comparison with another site (without targeting anyone) which is a reference for you, you will understand what I am talking about.

4. How to reduce the loading time of users when they join my Gmod server?

How to reduce the loading time of my users on my Gmod / DarkRP server or other gamemode?

- Reduce the number of workshop addons to what is strictly necessary for your project, do not have unnecessary and superfluous content downloaded, the longest loading time is there, the workshop addons.
- Install scripts downloaded from sources offering a minified version![/u] Here we will reduce the size of the downloaded Lua files.
- Don't overload your maps with badly optimized entities.

Minification is not the ultimate and micracle method to optimize your server if you are running with 200 workshop addons, it is only a step.
Applying minification on a whole program gives a result that is not negligible especially if it is downloaded during the connection of each user.
This method is used to reduce the download time in many languages.