Skip to content

  Spin up a managed Avalanche node cluster in minutes! Learn More.

The Sync Blog

Updates and insights from the Bloq team

« Blog Home

How-to: Compare Block Times in Bitcoin and Bitcoin Cash

compare block times

One way to get started with fundamental blockchain analytics is to compare the time between blocks. Longer than average block times in recent blocks results in higher transaction fees, longer confirmation times, and a larger mempool — that purgatory where unconfirmed transactions await inclusion in a block. 

Block times also reveal miner reactions to the most recent difficulty adjustment, like this one. Bitcoin’s mining difficulty adjusts roughly once every other week. Longer block times following a difficulty adjustment suggests that the new difficulty is too hard for miners. Shorter block times suggests that the difficulty is too easy.

You can compare block times across blockchains to highlight short term trends versus one another. For example: perhaps traction on Bitcoin Cash picks up with longer block times/larger bloat on Bitcoin, or vice versa. 

The sample code below allows users to analyze block times across recent blocks and between BTC and BCH.

Javascript Code Setup

For first time users, see the Getting Started guide to create your Bloq account and install the Javascript SDK.

Produce a new set of client keys with bcl client-keys create.

From there, create a javascript (.js) file and, in your text editor of choice, input the following code with your newly generated client keys:

The above code represents your document’s header. In the body below, we’ll put together the meat and potatoes of the code.

Start off your body with the .blocks method. This method grabs recent blocks and displays data points like the block height, hash, and difficulty. It also provides the block time, which is the variable we’ll rely on to figure out average block times:

Save the code as is (or clone from Github) and execute it in your command prompt with node file_name.js

You should see an output that lists the blocks returned, as well as additional info pertaining to the current date, time, and number of blocks returned.

Comparing Block Times

With the foundation set, the next steps involve modifying the body of the blocks method to calculate, package and return the average block time for the most recent n blocks.

First, add a limit value to the input. For now, ‘5’ will do:

Now, the program will only fetch the five most recent blocks. We need to iterate through each block and grab its corresponding block time. The list of blocks is also referred to as blocks, so to run through each block we’ll use a for loop that parses blocks.blocks:

Block time is represented as the time value for each block. Using our key to access every block, we can retrieve the corresponding block time with blocks.blocks[key].time. Use console.log() to see what that output reads as:

To get block times, we’ll have to find the difference in time between consecutive blocks. We’ll use a second counter variable that is offset with the key by one value. Then, subtract the two times to get the time between both. Keep in mind that the newest blocks are first displayed first, so the earlier key block time needs to be subtracted by the subsequent count block time.

Since we’re iterating through the blocks to record differences, we’ll always have one less recorded difference than number of blocks. We need to add some ‘checker’ that terminates the function once we’ve gotten our ‘n-1’ differences. To do this, add an if statement with a break command to end the loop one iteration early. There are five blocks to analyze, so the loop will naturally break when key hits a value of five. Set the if statement to break when key equals four:

The above function should properly print the differences in block times for the blocks queried through the .blocks method. To get the average block time, we need to sum each value and divide it by the total. With five blocks, we produce four numbers, as there are four differences between five values. 

Add a sum variable below count, increment sum with each block time, then divide it by the total to get your average:

Execute the above code and you’ll get the average block time from the five most recent Bitcoin blocks. However, that “average time” is output in seconds — not super helpful! Take the initial time and divide by 60 to get the time in minutes. Use parseInt() to drop the decimal component of the value. 

Then, take the same time and use modulus (%) by 60 to get the seconds. Drop the decimal with parseInt() like with the minutes and the average time can now be displayed as minutes and seconds. 

There you have it! Run the code as-is to get the average block time for the past five BTC blocks.

Further Improvements

Until this point, we have a working program. However, it’s not quite versatile. You can’t easily change the number of blocks to analyze or swap between BTC and BCH. We can fix this by wrapping the code and the above header parameters into the function that takes a ‘chain’ and ‘num’ parameter:

Perhaps you recognize this strategy from our “Query Two Blockchains” tutorial. Now that we’ve got the input variables, we need to replace each instance of constant values where the inputs belong- the ‘coin’ value in the header and each instance in the method where we use four or five. Let’s also modify the console.log output to reflect the chain being queried:

Now you can write any number of function calls below the document for whichever chain and number of blocks you’d like to see:

As one final touch, let’s add another console.log output that displays the time since the most recent block. In order to find the time since the most recent block, we’ll use Date.now() to grab the current time, then subtract that from the time of the last block at blocks.blocks[0].time. The same lines above can be copy/pasted to convert this time into minutes and seconds. Date.now() returns the local time in milliseconds, so we’ll first convert that to seconds by dividing it by 1,000:

And we’re done! This code is ready for you to utilize as-is or integrate into some larger project- such as a block explorer or BTC vs BCH chain comparison tool. 

Something to note:  the .blocks method will only return the blocks for the current date unless otherwise specified. You can modify the code further by incorporating a blockDate parameter in order to grab blocks across multiple dates or reflect on historic block times. Maybe you’ll find something interesting when you look at block times following difficulty re-adjustments or block halvings. 

Additionally, you can manipulate the code to look for other trends, like transaction sizes and coin velocity at points of interest, heightened mining fees, old address movements, and so on.

Join the Bloq newsletter for the latest updates

This field is for validation purposes and should be left unchanged.